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;
168 ret = av_frame_get_buffer(frame, 0);
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 }
522
523 ret = av_frame_ref(dst, src);
524 if (ret < 0)
525 return ret;
526
527finish:
528
529 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
530 ret = encode_generate_icc_profile(avctx, dst);
531 if (ret < 0)
532 return ret;
533 }
534
535 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
536 // since otherwise we cannot be sure that whatever value it has is in the
537 // right timebase, so we would produce an incorrect value, which is worse
538 // than none at all
539 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
540 dst->duration = 0;
541
542 return 0;
543}
544
546{
547 AVCodecInternal *avci = avctx->internal;
548 int ret;
549
550 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
551 return AVERROR(EINVAL);
552
553 if (avci->draining)
554 return AVERROR_EOF;
555
556 if (avci->buffer_frame->buf[0])
557 return AVERROR(EAGAIN);
558
559 if (!frame) {
560 avci->draining = 1;
561 } else {
562 ret = encode_send_frame_internal(avctx, frame);
563 if (ret < 0)
564 return ret;
565 }
566
567 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
568 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
569 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
570 return ret;
571 }
572
573 avctx->frame_num++;
574
575 return 0;
576}
577
579{
580 AVCodecInternal *avci = avctx->internal;
581 int ret;
582
583 av_packet_unref(avpkt);
584
585 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
586 return AVERROR(EINVAL);
587
588 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
589 av_packet_move_ref(avpkt, avci->buffer_pkt);
590 } else {
591 ret = encode_receive_packet_internal(avctx, avpkt);
592 if (ret < 0)
593 return ret;
594 }
595
596 return 0;
597}
598
600{
601 const AVCodec *codec = avctx->codec;
602 const FFCodec *codec2 = ffcodec(codec);
603 const AVDictionaryEntry *t = NULL;
605 int ret;
606
608
609 ret = av_dict_copy(&copy, *dict, 0);
610 if (ret < 0)
611 goto end;
612
613 // Remove the dictionary entries that would be applied to the private codec context
614 if (codec->priv_class) {
615 while ((t = av_dict_iterate(*dict, t))) {
616 if (av_opt_find(avctx->priv_data, t->key, NULL,
618 av_dict_set(dict, t->key, NULL, 0);
619 }
620 }
621
622 // Ditto for global options
623 if (codec2->defaults) {
624 const FFCodecDefault *d = codec2->defaults;
625 while (d->key) {
627 av_dict_set(dict, d->key, NULL, 0);
628 d++;
629 }
630 }
631
632 // If any entry remains, then the requested option/s don't exist or are not settable.
633 if (av_dict_count(*dict)) {
635 goto end;
636 }
637
638 ret = av_dict_copy(dict, copy, 0);
639 if (ret < 0)
640 goto end;
641
642 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
643 // one of them has an invalid value.
644 // This is done twice, once for avctx and once for the AVCodec, because using the
645 // search children flag in combination with the fake obj flag will iterate through
646 // the options from all compiled in codecs if you pass the avctx class.
647 if (codec->priv_class) {
648 ret = av_opt_set_dict2((void *)&codec->priv_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
649 if (ret < 0)
650 goto end;
651 }
652 ret = av_opt_set_dict2((void *)&avctx->av_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
653 if (ret < 0)
654 goto end;
655
656 // The dictionary should be empty.
658
659 ret = av_opt_set_dict2(avctx, dict, AV_OPT_SEARCH_CHILDREN);
660 if (ret < 0)
661 goto end;
662
663 // The dictionary should be empty.
664 av_assert0(!av_dict_count(*dict));
665
666 ret = 0;
667end:
669
670 return ret;
671}
672
674{
675 const FFCodec *codec = ffcodec(avctx->codec);
676 int ret = AVERROR_BUG;
677
678 if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
679 return AVERROR(EINVAL);
680
682 av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
683 return AVERROR(ENOSYS);
684 }
685
686 if (codec->reconf)
687 ret = codec->reconf(avctx, dict);
688 else
689 ret = ff_encode_reconf_parse_dict(avctx, dict);
690 if (ret < 0)
691 return ret;
692
693 return 0;
694}
695
697{
698 const AVCodec *c = avctx->codec;
699 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
700 const enum AVPixelFormat *pix_fmts;
701 int ret, i, num_pix_fmts;
702
703 if (!pixdesc) {
704 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
705 avctx->pix_fmt);
706 return AVERROR(EINVAL);
707 }
708
710 0, (const void **) &pix_fmts, &num_pix_fmts);
711 if (ret < 0)
712 return ret;
713
714 if (pix_fmts) {
715 for (i = 0; i < num_pix_fmts; i++)
716 if (avctx->pix_fmt == pix_fmts[i])
717 break;
718 if (i == num_pix_fmts) {
719 av_log(avctx, AV_LOG_ERROR,
720 "Specified pixel format %s is not supported by the %s encoder.\n",
721 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
722
723 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
724 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
725 av_log(avctx, AV_LOG_ERROR, " %s\n",
727 }
728
729 return AVERROR(EINVAL);
730 }
737 }
738
739 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
740 const enum AVAlphaMode *alpha_modes;
741 int num_alpha_modes;
743 0, (const void **) &alpha_modes, &num_alpha_modes);
744 if (ret < 0)
745 return ret;
746
747 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
748 for (i = 0; i < num_alpha_modes; i++) {
749 if (avctx->alpha_mode == alpha_modes[i])
750 break;
751 }
752 if (i == num_alpha_modes) {
753 av_log(avctx, AV_LOG_ERROR,
754 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
755 av_alpha_mode_name(avctx->alpha_mode), c->name);
756 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
757 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
758 av_log(avctx, AV_LOG_ERROR, " %s\n",
759 av_alpha_mode_name(alpha_modes[p]));
760 }
761 return AVERROR(EINVAL);
762 }
763 }
764 }
765
766 if ( avctx->bits_per_raw_sample < 0
767 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
768 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
769 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
770 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
771 }
772 if (avctx->width <= 0 || avctx->height <= 0) {
773 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
774 return AVERROR(EINVAL);
775 }
776
777 if (avctx->hw_frames_ctx) {
779 if (frames_ctx->format != avctx->pix_fmt) {
780 av_log(avctx, AV_LOG_ERROR,
781 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
782 return AVERROR(EINVAL);
783 }
784 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
785 avctx->sw_pix_fmt != frames_ctx->sw_format) {
786 av_log(avctx, AV_LOG_ERROR,
787 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
788 "and AVHWFramesContext.sw_format (%s)\n",
790 av_get_pix_fmt_name(frames_ctx->sw_format));
791 return AVERROR(EINVAL);
792 }
793 avctx->sw_pix_fmt = frames_ctx->sw_format;
794 }
795
796 return 0;
797}
798
800{
801 const AVCodec *c = avctx->codec;
802 const enum AVSampleFormat *sample_fmts;
803 const int *supported_samplerates;
804 const AVChannelLayout *ch_layouts;
805 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
806
807 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
808 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
809 avctx->sample_fmt);
810 return AVERROR(EINVAL);
811 }
812
814 0, (const void **) &sample_fmts,
815 &num_sample_fmts);
816 if (ret < 0)
817 return ret;
818 if (sample_fmts) {
819 for (i = 0; i < num_sample_fmts; i++) {
820 if (avctx->sample_fmt == sample_fmts[i])
821 break;
822 if (avctx->ch_layout.nb_channels == 1 &&
825 avctx->sample_fmt = sample_fmts[i];
826 break;
827 }
828 }
829 if (i == num_sample_fmts) {
830 av_log(avctx, AV_LOG_ERROR,
831 "Specified sample format %s is not supported by the %s encoder\n",
832 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
833
834 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
835 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
836 av_log(avctx, AV_LOG_ERROR, " %s\n",
838 }
839
840 return AVERROR(EINVAL);
841 }
842 }
843
845 0, (const void **) &supported_samplerates,
846 &num_samplerates);
847 if (ret < 0)
848 return ret;
849 if (supported_samplerates) {
850 for (i = 0; i < num_samplerates; i++)
851 if (avctx->sample_rate == supported_samplerates[i])
852 break;
853 if (i == num_samplerates) {
854 av_log(avctx, AV_LOG_ERROR,
855 "Specified sample rate %d is not supported by the %s encoder\n",
856 avctx->sample_rate, c->name);
857
858 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
859 for (int p = 0; supported_samplerates[p]; p++)
860 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
861
862 return AVERROR(EINVAL);
863 }
864 }
866 0, (const void **) &ch_layouts, &num_ch_layouts);
867 if (ret < 0)
868 return ret;
869 if (ch_layouts) {
870 for (i = 0; i < num_ch_layouts; i++) {
871 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
872 break;
873 }
874 if (i == num_ch_layouts) {
875 char buf[512];
876 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
877 av_log(avctx, AV_LOG_ERROR,
878 "Specified channel layout '%s' is not supported by the %s encoder\n",
879 ret > 0 ? buf : "?", c->name);
880
881 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
882 for (int p = 0; ch_layouts[p].nb_channels; p++) {
883 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
884 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
885 }
886 return AVERROR(EINVAL);
887 }
888 }
889
890 if (!avctx->bits_per_raw_sample)
892 if (!avctx->bits_per_raw_sample)
894
895 return 0;
896}
897
899{
900 AVCodecInternal *avci = avctx->internal;
901 EncodeContext *ec = encode_ctx(avci);
902 int ret = 0;
903
904 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
905 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
906 return AVERROR(EINVAL);
907 }
908
909 if (avctx->bit_rate < 0) {
910 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
911 return AVERROR(EINVAL);
912 }
913
914 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
916 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
917 "encoder does not support it.\n");
918 return AVERROR(EINVAL);
919 }
920
921 switch (avctx->codec_type) {
922 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
923 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
924 }
925 if (ret < 0)
926 return ret;
927
928 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
929 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
930 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
931 }
932
933 if (!avctx->rc_initial_buffer_occupancy)
934 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
935
938
940 avci->in_frame = av_frame_alloc();
941 if (!avci->in_frame)
942 return AVERROR(ENOMEM);
943 }
944
945 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
947 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
948 "from an encoder not supporting it\n");
949 return AVERROR(ENOSYS);
950 }
951
952 avci->recon_frame = av_frame_alloc();
953 if (!avci->recon_frame)
954 return AVERROR(ENOMEM);
955 }
956
957 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
958 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
959 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
960 const AVFrameSideData *sd_frame;
961 AVPacketSideData *sd_packet;
962
965 type_frame);
966 if (!sd_frame ||
968 type_packet))
969
970 continue;
971
973 type_packet, sd_frame->size, 0);
974 if (!sd_packet)
975 return AVERROR(ENOMEM);
976
977 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
978 }
979
980#if CONFIG_FRAME_THREAD_ENCODER
981 ret = ff_frame_thread_encoder_init(avctx);
982 if (ret < 0)
983 return ret;
984#endif
985
986 return 0;
987}
988
990{
991 int ret;
992
994
995 frame->format = avctx->pix_fmt;
996 if (frame->width <= 0 || frame->height <= 0) {
997 frame->width = avctx->width;
998 frame->height = avctx->height;
999 }
1000
1001 ret = avcodec_default_get_buffer2(avctx, frame, 0);
1002 if (ret < 0) {
1003 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1005 return ret;
1006 }
1007
1008 return 0;
1009}
1010
1012{
1013 AVCodecInternal *avci = avctx->internal;
1014
1015 if (!avci->recon_frame)
1016 return AVERROR(EINVAL);
1017 if (!avci->recon_frame->buf[0])
1018 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1019
1021 return 0;
1022}
1023
1025{
1026 AVCodecInternal *avci = avctx->internal;
1027
1028 if (avci->in_frame)
1029 av_frame_unref(avci->in_frame);
1030 if (avci->recon_frame)
1032}
1033
1035{
1036 return av_mallocz(sizeof(EncodeContext));
1037}
1038
1040{
1042 AVCPBProperties *props;
1043 size_t size;
1044 int i;
1045
1046 for (i = 0; i < avctx->nb_coded_side_data; i++)
1048 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1049
1050 props = av_cpb_properties_alloc(&size);
1051 if (!props)
1052 return NULL;
1053
1054 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1055 if (!tmp) {
1056 av_freep(&props);
1057 return NULL;
1058 }
1059
1060 avctx->coded_side_data = tmp;
1061 avctx->nb_coded_side_data++;
1062
1064 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1065 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1066
1067 return props;
1068}
1069
1071 int error_count, enum AVPictureType pict_type)
1072{
1073 uint8_t *side_data;
1074 size_t side_data_size;
1075
1076 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1077 if (!side_data) {
1078 side_data_size = 4+4+8*error_count;
1080 side_data_size);
1081 }
1082
1083 if (!side_data || side_data_size < 4+4+8*error_count)
1084 return AVERROR(ENOMEM);
1085
1086 AV_WL32(side_data, quality);
1087 side_data[4] = pict_type;
1088 side_data[5] = error_count;
1089 for (int i = 0; i < error_count; ++i)
1090 AV_WL64(side_data+8 + 8*i , error[i]);
1091
1092 return 0;
1093}
1094
1095int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1096{
1097 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1098 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1099 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1100 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1101 uint16_t *matrix = matrices[m];
1102 if (matrix && (types & (1U << m))) {
1103 for (int i = 0; i < 64; i++) {
1104 if (matrix[i] < min || matrix[i] > max) {
1105 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);
1106 return AVERROR(EINVAL);
1107 }
1108 }
1109 }
1110 }
1111 return 0;
1112}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
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:975
AVAudioServiceType
Definition defs.h:235
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:799
static int encode_preinit_video(AVCodecContext *avctx)
Definition encode.c:696
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition encode.c:1039
AVCodecInternal * ff_encode_internal_alloc(void)
Definition encode.c:1034
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:898
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:1070
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:1011
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition encode.c:989
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition encode.c:1095
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
Definition encode.c:599
void ff_encode_flush_buffers(AVCodecContext *avctx)
Definition encode.c:1024
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
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:673
#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
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:578
#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:545
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:818
@ AV_CODEC_CONFIG_PIX_FORMAT
AVPixelFormat, terminated by AV_PIX_FMT_NONE.
Definition avcodec.h:2573
@ AV_CODEC_CONFIG_SAMPLE_FORMAT
AVSampleFormat, terminated by AV_SAMPLE_FMT_NONE.
Definition avcodec.h:2576
@ AV_CODEC_CONFIG_ALPHA_MODE
AVAlphaMode, terminated by AVALPHA_MODE_UNSPECIFIED.
Definition avcodec.h:2580
@ AV_CODEC_CONFIG_SAMPLE_RATE
int, terminated by 0
Definition avcodec.h:2575
@ AV_CODEC_CONFIG_CHANNEL_LAYOUT
AVChannelLayout, terminated by {0}.
Definition avcodec.h:2577
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:702
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:620
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:570
@ 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:394
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
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:650
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
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:183
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:233
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:247
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:86
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:37
#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:1196
@ 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:217
@ 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:108
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition samplefmt.c:86
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:51
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:246
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:222
#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:2069
#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:2040
#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:170
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:143
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:50
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
const char data[16]
Definition mxf.c:149
int profile
Definition mxfenc.c:2299
AVOptions.
const char * av_alpha_mode_name(enum AVAlphaMode mode)
Definition pixdesc.c:3925
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:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:816
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition pixfmt.h:817
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:642
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
#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:282
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:1768
char * stats_out
pass1 encoding statistics output buffer
Definition avcodec.h:1330
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition avcodec.h:1709
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:1769
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:1471
enum AVMediaType codec_type
Definition avcodec.h:451
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
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:1872
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:1930
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
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:1929
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition avcodec.h:1937
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:327
size_t size
Definition frame.h:330
uint8_t * data
Definition frame.h:329
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition frame.h:649
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:424
uint8_t * data
Definition packet.h:425
enum AVPacketSideDataType type
Definition packet.h:427
This structure stores compressed data.
Definition packet.h:580
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:586
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:621
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition packet.h:614
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:2089
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]