FFmpeg  4.4
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/attributes.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/frame.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/samplefmt.h"
27 
28 #include "avcodec.h"
29 #include "encode.h"
30 #include "frame_thread_encoder.h"
31 #include "internal.h"
32 
33 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
34 {
35  if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
36  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
38  return AVERROR(EINVAL);
39  }
40 
41  av_assert0(!avpkt->data);
42 
43  if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
45  avpkt->data = avctx->internal->byte_buffer;
46  avpkt->size = size;
47  }
48 
49  if (!avpkt->data) {
50  int ret = av_new_packet(avpkt, size);
51  if (ret < 0)
52  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
53  return ret;
54  }
55 
56  return 0;
57 }
58 
60 {
61  int ret;
62 
63  if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
64  return AVERROR(EINVAL);
65 
66  if (avpkt->data || avpkt->buf) {
67  av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
68  return AVERROR(EINVAL);
69  }
70 
71  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
72  if (ret < 0) {
73  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
74  return ret;
75  }
76  avpkt->data = avpkt->buf->data;
77  memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
78 
79  return 0;
80 }
81 
82 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
83 {
84  int ret;
85 
86  if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
87  return AVERROR(EINVAL);
88 
89  av_assert0(!avpkt->data && !avpkt->buf);
90 
91  avpkt->size = size;
92  ret = avctx->get_encode_buffer(avctx, avpkt, flags);
93  if (ret < 0)
94  goto fail;
95 
96  if (!avpkt->data || !avpkt->buf) {
97  av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
98  ret = AVERROR(EINVAL);
99  goto fail;
100  }
101 
102  ret = 0;
103 fail:
104  if (ret < 0) {
105  av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
106  av_packet_unref(avpkt);
107  }
108 
109  return ret;
110 }
111 
112 /**
113  * Pad last frame with silence.
114  */
116 {
117  int ret;
118 
119  frame->format = src->format;
120  frame->channel_layout = src->channel_layout;
121  frame->channels = src->channels;
122  frame->nb_samples = s->frame_size;
123  ret = av_frame_get_buffer(frame, 0);
124  if (ret < 0)
125  goto fail;
126 
127  ret = av_frame_copy_props(frame, src);
128  if (ret < 0)
129  goto fail;
130 
131  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
132  src->nb_samples, s->channels, s->sample_fmt)) < 0)
133  goto fail;
134  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
135  frame->nb_samples - src->nb_samples,
136  s->channels, s->sample_fmt)) < 0)
137  goto fail;
138 
139  return 0;
140 
141 fail:
143  return ret;
144 }
145 
146 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
147  const AVSubtitle *sub)
148 {
149  int ret;
150  if (sub->start_display_time) {
151  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
152  return -1;
153  }
154 
155  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
156  avctx->frame_number++;
157  return ret;
158 }
159 
161 {
162  AVCodecInternal *avci = avctx->internal;
163 
164  if (avci->draining)
165  return AVERROR_EOF;
166 
167  if (!avci->buffer_frame->buf[0])
168  return AVERROR(EAGAIN);
169 
171 
172  return 0;
173 }
174 
176 {
177  AVCodecInternal *avci = avctx->internal;
178  EncodeSimpleContext *es = &avci->es;
179  AVFrame *frame = es->in_frame;
180  int got_packet;
181  int ret;
182 
183  if (avci->draining_done)
184  return AVERROR_EOF;
185 
186  if (!frame->buf[0] && !avci->draining) {
188  ret = ff_encode_get_frame(avctx, frame);
189  if (ret < 0 && ret != AVERROR_EOF)
190  return ret;
191  }
192 
193  if (!frame->buf[0]) {
194  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
196  return AVERROR_EOF;
197 
198  // Flushing is signaled with a NULL frame
199  frame = NULL;
200  }
201 
202  got_packet = 0;
203 
204  av_assert0(avctx->codec->encode2);
205 
208  /* This might modify frame, but it doesn't matter, because
209  * the frame properties used below are not used for video
210  * (due to the delay inherent in frame threaded encoding, it makes
211  * no sense to use the properties of the current frame anyway). */
212  ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
213  else {
214  ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
215  if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
217  avpkt->pts = avpkt->dts = frame->pts;
218  }
219 
220  av_assert0(ret <= 0);
221 
222  emms_c();
223 
224  if (!ret && got_packet) {
225  if (avpkt->data) {
226  ret = av_packet_make_refcounted(avpkt);
227  if (ret < 0)
228  goto end;
229  }
230 
231  if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
232  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
233  if (avpkt->pts == AV_NOPTS_VALUE)
234  avpkt->pts = frame->pts;
235  if (!avpkt->duration)
236  avpkt->duration = ff_samples_to_time_base(avctx,
237  frame->nb_samples);
238  }
239  }
240  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
241  /* NOTE: if we add any audio encoders which output non-keyframe packets,
242  * this needs to be moved to the encoders, but for now we can do it
243  * here to simplify things */
244  avpkt->flags |= AV_PKT_FLAG_KEY;
245  avpkt->dts = avpkt->pts;
246  }
247  }
248 
249  if (avci->draining && !got_packet)
250  avci->draining_done = 1;
251 
252 end:
253  if (ret < 0 || !got_packet)
254  av_packet_unref(avpkt);
255 
256  if (frame) {
257  if (!ret)
258  avctx->frame_number++;
260  }
261 
262  if (got_packet)
263  // Encoders must always return ref-counted buffers.
264  // Side-data only packets have no data and can be not ref-counted.
265  av_assert0(!avpkt->data || avpkt->buf);
266 
267  return ret;
268 }
269 
271 {
272  int ret;
273 
274  while (!avpkt->data && !avpkt->side_data) {
275  ret = encode_simple_internal(avctx, avpkt);
276  if (ret < 0)
277  return ret;
278  }
279 
280  return 0;
281 }
282 
284 {
285  AVCodecInternal *avci = avctx->internal;
286  int ret;
287 
288  if (avci->draining_done)
289  return AVERROR_EOF;
290 
291  av_assert0(!avpkt->data && !avpkt->side_data);
292 
293  if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
294  if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
295  avctx->stats_out[0] = '\0';
296  if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
297  return AVERROR(EINVAL);
298  }
299 
300  if (avctx->codec->receive_packet) {
301  ret = avctx->codec->receive_packet(avctx, avpkt);
302  if (ret < 0)
303  av_packet_unref(avpkt);
304  else
305  // Encoders must always return ref-counted buffers.
306  // Side-data only packets have no data and can be not ref-counted.
307  av_assert0(!avpkt->data || avpkt->buf);
308  } else
309  ret = encode_simple_receive_packet(avctx, avpkt);
310 
311  if (ret == AVERROR_EOF)
312  avci->draining_done = 1;
313 
314  return ret;
315 }
316 
318 {
319  AVCodecInternal *avci = avctx->internal;
320  AVFrame *dst = avci->buffer_frame;
321  int ret;
322 
323  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
324  /* extract audio service type metadata */
326  if (sd && sd->size >= sizeof(enum AVAudioServiceType))
327  avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
328 
329  /* check for valid frame size */
331  if (src->nb_samples > avctx->frame_size) {
332  av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
333  return AVERROR(EINVAL);
334  }
335  } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
336  /* if we already got an undersized frame, that must have been the last */
337  if (avctx->internal->last_audio_frame) {
338  av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
339  return AVERROR(EINVAL);
340  }
341 
342  if (src->nb_samples < avctx->frame_size) {
343  ret = pad_last_frame(avctx, dst, src);
344  if (ret < 0)
345  return ret;
346 
347  avctx->internal->last_audio_frame = 1;
348  } else if (src->nb_samples > avctx->frame_size) {
349  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
350  return AVERROR(EINVAL);
351  }
352  }
353  }
354 
355  if (!dst->data[0]) {
356  ret = av_frame_ref(dst, src);
357  if (ret < 0)
358  return ret;
359  }
360 
361  return 0;
362 }
363 
365 {
366  AVCodecInternal *avci = avctx->internal;
367  int ret;
368 
369  if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
370  return AVERROR(EINVAL);
371 
372  if (avci->draining)
373  return AVERROR_EOF;
374 
375  if (avci->buffer_frame->data[0])
376  return AVERROR(EAGAIN);
377 
378  if (!frame) {
379  avci->draining = 1;
380  } else {
381  ret = encode_send_frame_internal(avctx, frame);
382  if (ret < 0)
383  return ret;
384  }
385 
386  if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
387  ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
388  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
389  return ret;
390  }
391 
392  return 0;
393 }
394 
396 {
397  AVCodecInternal *avci = avctx->internal;
398  int ret;
399 
400  av_packet_unref(avpkt);
401 
402  if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
403  return AVERROR(EINVAL);
404 
405  if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
406  av_packet_move_ref(avpkt, avci->buffer_pkt);
407  } else {
408  ret = encode_receive_packet_internal(avctx, avpkt);
409  if (ret < 0)
410  return ret;
411  }
412 
413  return 0;
414 }
415 
416 #if FF_API_OLD_ENCDEC
417 static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
418  int *got_packet, const AVFrame *frame)
419 {
420  AVCodecInternal *avci = avctx->internal;
421  AVPacket user_pkt;
422  int ret;
423 
424  *got_packet = 0;
425 
426  if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
427  if (frame->format == AV_PIX_FMT_NONE)
428  av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
429  if (frame->width == 0 || frame->height == 0)
430  av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
431  }
432 
433  if (avctx->codec->capabilities & AV_CODEC_CAP_DR1) {
434  av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* API does not support "
435  "AV_CODEC_CAP_DR1 encoders\n");
436  return AVERROR(ENOSYS);
437  }
438 
439  ret = avcodec_send_frame(avctx, frame);
440  if (ret == AVERROR_EOF)
441  ret = 0;
442  else if (ret == AVERROR(EAGAIN)) {
443  /* we fully drain all the output in each encode call, so this should not
444  * ever happen */
445  return AVERROR_BUG;
446  } else if (ret < 0)
447  return ret;
448 
449  av_packet_move_ref(&user_pkt, avpkt);
450  while (ret >= 0) {
451  ret = avcodec_receive_packet(avctx, avpkt);
452  if (ret < 0) {
453  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
454  ret = 0;
455  goto finish;
456  }
457 
458  if (avpkt != avci->compat_encode_packet) {
459  if (avpkt->data && user_pkt.data) {
460  if (user_pkt.size >= avpkt->size) {
461  memcpy(user_pkt.data, avpkt->data, avpkt->size);
462  av_buffer_unref(&avpkt->buf);
463  avpkt->buf = user_pkt.buf;
464  avpkt->data = user_pkt.data;
466  av_init_packet(&user_pkt);
468  } else {
469  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
470  av_packet_unref(avpkt);
471  ret = AVERROR(EINVAL);
472  goto finish;
473  }
474  }
475 
476  *got_packet = 1;
477  avpkt = avci->compat_encode_packet;
478  } else {
479  if (!avci->compat_decode_warned) {
480  av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
481  "API cannot return all the packets for this encoder. "
482  "Some packets will be dropped. Update your code to the "
483  "new encoding API to fix this.\n");
484  avci->compat_decode_warned = 1;
485  av_packet_unref(avpkt);
486  }
487  }
488 
489  if (avci->draining)
490  break;
491  }
492 
493 finish:
494  if (ret < 0)
495  av_packet_unref(&user_pkt);
496 
497  return ret;
498 }
499 
501  AVPacket *avpkt,
502  const AVFrame *frame,
503  int *got_packet_ptr)
504 {
505  int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
506 
507  if (ret < 0)
508  av_packet_unref(avpkt);
509 
510  return ret;
511 }
512 
514  AVPacket *avpkt,
515  const AVFrame *frame,
516  int *got_packet_ptr)
517 {
518  int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
519 
520  if (ret < 0)
521  av_packet_unref(avpkt);
522 
523  return ret;
524 }
525 #endif
526 
528 {
529  int i;
530 #if FF_API_CODED_FRAME
532  avctx->coded_frame = av_frame_alloc();
533  if (!avctx->coded_frame) {
534  return AVERROR(ENOMEM);
535  }
537 #endif
538 
539  if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
540  av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
541  return AVERROR(EINVAL);
542  }
543 
544  if (avctx->codec->sample_fmts) {
545  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
546  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
547  break;
548  if (avctx->channels == 1 &&
551  avctx->sample_fmt = avctx->codec->sample_fmts[i];
552  break;
553  }
554  }
555  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
556  char buf[128];
557  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
558  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
559  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
560  return AVERROR(EINVAL);
561  }
562  }
563  if (avctx->codec->pix_fmts) {
564  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
565  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
566  break;
567  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
568  && !(avctx->codec_id == AV_CODEC_ID_MJPEG
570  char buf[128];
571  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
572  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
573  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
574  return AVERROR(EINVAL);
575  }
576  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
577  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
578  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
579  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
580  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
581  avctx->color_range = AVCOL_RANGE_JPEG;
582  }
583  if (avctx->codec->supported_samplerates) {
584  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
585  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
586  break;
587  if (avctx->codec->supported_samplerates[i] == 0) {
588  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
589  avctx->sample_rate);
590  return AVERROR(EINVAL);
591  }
592  }
593  if (avctx->sample_rate < 0) {
594  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
595  avctx->sample_rate);
596  return AVERROR(EINVAL);
597  }
598  if (avctx->codec->channel_layouts) {
599  if (!avctx->channel_layout) {
600  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
601  } else {
602  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
603  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
604  break;
605  if (avctx->codec->channel_layouts[i] == 0) {
606  char buf[512];
607  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
608  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
609  return AVERROR(EINVAL);
610  }
611  }
612  }
613  if (avctx->channel_layout && avctx->channels) {
615  if (channels != avctx->channels) {
616  char buf[512];
617  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
618  av_log(avctx, AV_LOG_ERROR,
619  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
620  buf, channels, avctx->channels);
621  return AVERROR(EINVAL);
622  }
623  } else if (avctx->channel_layout) {
625  }
626  if (avctx->channels < 0) {
627  av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
628  avctx->channels);
629  return AVERROR(EINVAL);
630  }
631  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
632  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
633  if ( avctx->bits_per_raw_sample < 0
634  || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
635  av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
636  avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
637  avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
638  }
639  if (avctx->width <= 0 || avctx->height <= 0) {
640  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
641  return AVERROR(EINVAL);
642  }
643  }
644  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
645  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
646  av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
647  }
648 
649  if (!avctx->rc_initial_buffer_occupancy)
650  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
651 
652  if (avctx->ticks_per_frame && avctx->time_base.num &&
653  avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
654  av_log(avctx, AV_LOG_ERROR,
655  "ticks_per_frame %d too large for the timebase %d/%d.",
656  avctx->ticks_per_frame,
657  avctx->time_base.num,
658  avctx->time_base.den);
659  return AVERROR(EINVAL);
660  }
661 
662  if (avctx->hw_frames_ctx) {
663  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
664  if (frames_ctx->format != avctx->pix_fmt) {
665  av_log(avctx, AV_LOG_ERROR,
666  "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
667  return AVERROR(EINVAL);
668  }
669  if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
670  avctx->sw_pix_fmt != frames_ctx->sw_format) {
671  av_log(avctx, AV_LOG_ERROR,
672  "Mismatching AVCodecContext.sw_pix_fmt (%s) "
673  "and AVHWFramesContext.sw_format (%s)\n",
675  av_get_pix_fmt_name(frames_ctx->sw_format));
676  return AVERROR(EINVAL);
677  }
678  avctx->sw_pix_fmt = frames_ctx->sw_format;
679  }
680 
681  return 0;
682 }
channels
Definition: aptx.h:33
Macro definitions for various function/variable attributes.
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:1605
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1784
void av_init_packet(AVPacket *pkt)
Definition: avpacket.c:36
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define CONFIG_FRAME_THREAD_ENCODER
Definition: config.h:639
#define NULL
Definition: coverity.c:32
static AVFrame * frame
static float sub(float src0, float src1)
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:82
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:160
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
Definition: encode.c:317
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
int ff_encode_preinit(AVCodecContext *avctx)
Definition: encode.c:527
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:283
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:270
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:175
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
Pad last frame with silence.
Definition: encode.c:115
static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt, int *got_packet, const AVFrame *frame)
Definition: encode.c:417
reference-counted frame API
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet_ptr)
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:74
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:129
#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:77
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:296
AVAudioServiceType
Definition: avcodec.h:239
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:82
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:395
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
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:364
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
The default callback for AVCodecContext.get_encode_buffer().
Definition: encode.c:59
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: encode.c:513
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: encode.c:146
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: encode.c:500
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:50
int avcodec_is_open(AVCodecContext *s)
Definition: avcodec.c:848
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:696
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:690
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
int av_buffer_realloc(AVBufferRef **pbuf, buffer_size_t size)
Reallocate a given buffer.
Definition: buffer.c:169
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:337
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:582
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:443
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:738
@ 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:113
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:308
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:288
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:49
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition: samplefmt.c:84
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
int av_samples_copy(uint8_t **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:213
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:237
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
misc image utilities
int i
Definition: input.c:407
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:272
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define attribute_align_arg
Definition: internal.h:61
#define emms_c()
Definition: internal.h:54
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:2489
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ 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:100
@ 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:258
@ 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:79
@ 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:80
@ 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:78
#define snprintf
Definition: snprintf.h:34
uint8_t * data
The data buffer.
Definition: buffer.h:92
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1557
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1401
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:2078
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:2248
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1601
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1261
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:2218
enum AVMediaType codec_type
Definition: avcodec.h:544
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1764
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:668
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1792
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:2384
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
const struct AVCodec * codec
Definition: avcodec.h:545
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1444
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1747
int sample_rate
samples per second
Definition: avcodec.h:1196
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1227
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:659
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
int channels
number of audio channels
Definition: avcodec.h:1197
enum AVCodecID codec_id
Definition: avcodec.h:546
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
unsigned int byte_buffer_size
Definition: internal.h:161
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
Definition: internal.h:160
EncodeSimpleContext es
Definition: internal.h:165
int last_audio_frame
An audio frame with less than required samples has been submitted and padded with silence.
Definition: internal.h:137
AVFrame * buffer_frame
Definition: internal.h:186
AVPacket * buffer_pkt
buffers for using new encode/decode API through legacy API
Definition: internal.h:185
int draining
checks API usage: after codec draining, flush is required to resume operation
Definition: internal.h:180
void * frame_thread_encoder
Definition: internal.h:163
AVPacket * compat_encode_packet
Definition: internal.h:198
int compat_decode_warned
Definition: internal.h:190
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
Definition: codec.h:221
int(* receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt)
Encode API with decoupled frame/packet dataflow.
Definition: codec.h:311
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:220
enum AVMediaType type
Definition: codec.h:210
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:218
int(* encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt, const struct AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
Definition: codec.h:289
int(* encode_sub)(struct AVCodecContext *, uint8_t *buf, int buf_size, const struct AVSubtitle *sub)
Definition: codec.h:277
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0
Definition: codec.h:219
int capabilities
Codec capabilities.
Definition: codec.h:216
int depth
Number of bits in the component.
Definition: pixdesc.h:58
Structure to hold side data for an AVFrame.
Definition: frame.h:220
uint8_t * data
Definition: frame.h:222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int channels
number of audio channels, only used for audio.
Definition: frame.h:624
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:495
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:365
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
This structure stores compressed data.
Definition: packet.h:346
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:352
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:380
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
AVFrame * in_frame
Definition: internal.h:121
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
static void finish(void)
Definition: movenc.c:342
int size
if(ret< 0)
Definition: vf_mcdeint.c:282