FFmpeg  4.4.9
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/hwcontext.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/samplefmt.h"
28 
29 #include "avcodec.h"
30 #include "encode.h"
31 #include "frame_thread_encoder.h"
32 #include "internal.h"
33 
35 {
36  if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
37  av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
39  return AVERROR(EINVAL);
40  }
41 
42  av_assert0(!avpkt->data);
43 
44  if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
46  avpkt->data = avctx->internal->byte_buffer;
47  avpkt->size = size;
48  }
49 
50  if (!avpkt->data) {
51  int ret = av_new_packet(avpkt, size);
52  if (ret < 0)
53  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
54  return ret;
55  }
56 
57  return 0;
58 }
59 
61 {
62  int ret;
63 
64  if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
65  return AVERROR(EINVAL);
66 
67  if (avpkt->data || avpkt->buf) {
68  av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
69  return AVERROR(EINVAL);
70  }
71 
72  ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
73  if (ret < 0) {
74  av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
75  return ret;
76  }
77  avpkt->data = avpkt->buf->data;
78 
79  return 0;
80 }
81 
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  memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
102 
103  ret = 0;
104 fail:
105  if (ret < 0) {
106  av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
107  av_packet_unref(avpkt);
108  }
109 
110  return ret;
111 }
112 
113 /**
114  * Pad last frame with silence.
115  */
117 {
118  int ret;
119 
120  frame->format = src->format;
121  frame->channel_layout = src->channel_layout;
122  frame->channels = src->channels;
123  frame->nb_samples = s->frame_size;
124  ret = av_frame_get_buffer(frame, 0);
125  if (ret < 0)
126  goto fail;
127 
128  ret = av_frame_copy_props(frame, src);
129  if (ret < 0)
130  goto fail;
131 
132  if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
133  src->nb_samples, s->channels, s->sample_fmt)) < 0)
134  goto fail;
135  if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
136  frame->nb_samples - src->nb_samples,
137  s->channels, s->sample_fmt)) < 0)
138  goto fail;
139 
140  return 0;
141 
142 fail:
144  return ret;
145 }
146 
147 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
148  const AVSubtitle *sub)
149 {
150  int ret;
151  if (sub->start_display_time) {
152  av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
153  return -1;
154  }
155  av_assert0(buf_size >= 0);
156 
157  ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
158  avctx->frame_number++;
159  return ret;
160 }
161 
163 {
164  AVCodecInternal *avci = avctx->internal;
165 
166  if (avci->draining)
167  return AVERROR_EOF;
168 
169  if (!avci->buffer_frame->buf[0])
170  return AVERROR(EAGAIN);
171 
173 
174  return 0;
175 }
176 
178 {
179  AVCodecInternal *avci = avctx->internal;
180  EncodeSimpleContext *es = &avci->es;
181  AVFrame *frame = es->in_frame;
182  int got_packet;
183  int ret;
184 
185  if (avci->draining_done)
186  return AVERROR_EOF;
187 
188  if (!frame->buf[0] && !avci->draining) {
190  ret = ff_encode_get_frame(avctx, frame);
191  if (ret < 0 && ret != AVERROR_EOF)
192  return ret;
193  }
194 
195  if (!frame->buf[0]) {
196  if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
198  return AVERROR_EOF;
199 
200  // Flushing is signaled with a NULL frame
201  frame = NULL;
202  }
203 
204  got_packet = 0;
205 
206  av_assert0(avctx->codec->encode2);
207 
210  /* This might modify frame, but it doesn't matter, because
211  * the frame properties used below are not used for video
212  * (due to the delay inherent in frame threaded encoding, it makes
213  * no sense to use the properties of the current frame anyway). */
214  ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
215  else {
216  ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
217  if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
219  avpkt->pts = avpkt->dts = frame->pts;
220  }
221 
222  av_assert0(ret <= 0);
223 
224  emms_c();
225 
226  if (!ret && got_packet) {
227  if (avpkt->data) {
228  ret = av_packet_make_refcounted(avpkt);
229  if (ret < 0)
230  goto end;
231  }
232 
233  if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
234  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
235  if (avpkt->pts == AV_NOPTS_VALUE)
236  avpkt->pts = frame->pts;
237  if (!avpkt->duration)
238  avpkt->duration = ff_samples_to_time_base(avctx,
239  frame->nb_samples);
240  }
241  }
242  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
243  /* NOTE: if we add any audio encoders which output non-keyframe packets,
244  * this needs to be moved to the encoders, but for now we can do it
245  * here to simplify things */
246  avpkt->flags |= AV_PKT_FLAG_KEY;
247  avpkt->dts = avpkt->pts;
248  }
249  }
250 
251  if (avci->draining && !got_packet)
252  avci->draining_done = 1;
253 
254 end:
255  if (ret < 0 || !got_packet)
256  av_packet_unref(avpkt);
257 
258  if (frame) {
259  if (!ret)
260  avctx->frame_number++;
262  }
263 
264  if (got_packet)
265  // Encoders must always return ref-counted buffers.
266  // Side-data only packets have no data and can be not ref-counted.
267  av_assert0(!avpkt->data || avpkt->buf);
268 
269  return ret;
270 }
271 
273 {
274  int ret;
275 
276  while (!avpkt->data && !avpkt->side_data) {
277  ret = encode_simple_internal(avctx, avpkt);
278  if (ret < 0)
279  return ret;
280  }
281 
282  return 0;
283 }
284 
286 {
287  AVCodecInternal *avci = avctx->internal;
288  int ret;
289 
290  if (avci->draining_done)
291  return AVERROR_EOF;
292 
293  av_assert0(!avpkt->data && !avpkt->side_data);
294 
295  if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
296  if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
297  avctx->stats_out[0] = '\0';
298  if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
299  return AVERROR(EINVAL);
300  }
301 
302  if (avctx->codec->receive_packet) {
303  ret = avctx->codec->receive_packet(avctx, avpkt);
304  if (ret < 0)
305  av_packet_unref(avpkt);
306  else
307  // Encoders must always return ref-counted buffers.
308  // Side-data only packets have no data and can be not ref-counted.
309  av_assert0(!avpkt->data || avpkt->buf);
310  } else
311  ret = encode_simple_receive_packet(avctx, avpkt);
312 
313  if (ret == AVERROR_EOF)
314  avci->draining_done = 1;
315 
316  return ret;
317 }
318 
320 {
321  AVCodecInternal *avci = avctx->internal;
322  AVFrame *dst = avci->buffer_frame;
323  int ret;
324 
325  if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
326  /* extract audio service type metadata */
328  if (sd && sd->size >= sizeof(enum AVAudioServiceType))
329  avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
330 
331  /* check for valid frame size */
333  if (src->nb_samples > avctx->frame_size) {
334  av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
335  return AVERROR(EINVAL);
336  }
337  } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
338  /* if we already got an undersized frame, that must have been the last */
339  if (avctx->internal->last_audio_frame) {
340  av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
341  return AVERROR(EINVAL);
342  }
343 
344  if (src->nb_samples < avctx->frame_size) {
345  ret = pad_last_frame(avctx, dst, src);
346  if (ret < 0)
347  return ret;
348 
349  avctx->internal->last_audio_frame = 1;
350  } else if (src->nb_samples > avctx->frame_size) {
351  av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
352  return AVERROR(EINVAL);
353  }
354  }
355  } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
357 
358  if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
359  if (src->hw_frames_ctx) {
360  const AVHWFramesContext *frames_ctx = (AVHWFramesContext *)src->hw_frames_ctx->data;
361 
362  if (frames_ctx->width < avctx->width || frames_ctx->height < avctx->height) {
363  av_log(avctx, AV_LOG_ERROR, "Hardware frames are smaller than the encoder\n");
364  return AVERROR(EINVAL);
365  }
366  }
367  } else if (src->format != avctx->pix_fmt ||
368  src->width != avctx->width || src->height != avctx->height) {
369  av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
370  return AVERROR(EINVAL);
371  }
372  }
373 
374  if (!dst->data[0]) {
375  ret = av_frame_ref(dst, src);
376  if (ret < 0)
377  return ret;
378  }
379 
380  return 0;
381 }
382 
384 {
385  AVCodecInternal *avci = avctx->internal;
386  int ret;
387 
388  if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
389  return AVERROR(EINVAL);
390 
391  if (avci->draining)
392  return AVERROR_EOF;
393 
394  if (avci->buffer_frame->buf[0])
395  return AVERROR(EAGAIN);
396 
397  if (!frame) {
398  avci->draining = 1;
399  } else {
400  ret = encode_send_frame_internal(avctx, frame);
401  if (ret < 0)
402  return ret;
403  }
404 
405  if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
406  ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
407  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
408  return ret;
409  }
410 
411  return 0;
412 }
413 
415 {
416  AVCodecInternal *avci = avctx->internal;
417  int ret;
418 
419  av_packet_unref(avpkt);
420 
421  if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
422  return AVERROR(EINVAL);
423 
424  if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
425  av_packet_move_ref(avpkt, avci->buffer_pkt);
426  } else {
427  ret = encode_receive_packet_internal(avctx, avpkt);
428  if (ret < 0)
429  return ret;
430  }
431 
432  return 0;
433 }
434 
435 #if FF_API_OLD_ENCDEC
436 static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
437  int *got_packet, const AVFrame *frame)
438 {
439  AVCodecInternal *avci = avctx->internal;
440  AVPacket user_pkt;
441  int ret;
442 
443  *got_packet = 0;
444 
445  if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
446  if (frame->format == AV_PIX_FMT_NONE)
447  av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
448  if (frame->width == 0 || frame->height == 0)
449  av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
450  }
451 
452  if (avctx->codec->capabilities & AV_CODEC_CAP_DR1) {
453  av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* API does not support "
454  "AV_CODEC_CAP_DR1 encoders\n");
455  return AVERROR(ENOSYS);
456  }
457 
458  ret = avcodec_send_frame(avctx, frame);
459  if (ret == AVERROR_EOF)
460  ret = 0;
461  else if (ret == AVERROR(EAGAIN)) {
462  /* we fully drain all the output in each encode call, so this should not
463  * ever happen */
464  return AVERROR_BUG;
465  } else if (ret < 0)
466  return ret;
467 
468  av_packet_move_ref(&user_pkt, avpkt);
469  while (ret >= 0) {
470  ret = avcodec_receive_packet(avctx, avpkt);
471  if (ret < 0) {
472  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
473  ret = 0;
474  goto finish;
475  }
476 
477  if (avpkt != avci->compat_encode_packet) {
478  if (avpkt->data && user_pkt.data) {
479  if (user_pkt.size >= avpkt->size) {
480  memcpy(user_pkt.data, avpkt->data, avpkt->size);
481  av_buffer_unref(&avpkt->buf);
482  avpkt->buf = user_pkt.buf;
483  avpkt->data = user_pkt.data;
485  av_init_packet(&user_pkt);
487  } else {
488  av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
489  av_packet_unref(avpkt);
490  ret = AVERROR(EINVAL);
491  goto finish;
492  }
493  }
494 
495  *got_packet = 1;
496  avpkt = avci->compat_encode_packet;
497  } else {
498  if (!avci->compat_decode_warned) {
499  av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
500  "API cannot return all the packets for this encoder. "
501  "Some packets will be dropped. Update your code to the "
502  "new encoding API to fix this.\n");
503  avci->compat_decode_warned = 1;
504  av_packet_unref(avpkt);
505  }
506  }
507 
508  if (avci->draining)
509  break;
510  }
511 
512 finish:
513  if (ret < 0)
514  av_packet_unref(&user_pkt);
515 
516  return ret;
517 }
518 
520  AVPacket *avpkt,
521  const AVFrame *frame,
522  int *got_packet_ptr)
523 {
524  int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
525 
526  if (ret < 0)
527  av_packet_unref(avpkt);
528 
529  return ret;
530 }
531 
533  AVPacket *avpkt,
534  const AVFrame *frame,
535  int *got_packet_ptr)
536 {
537  int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
538 
539  if (ret < 0)
540  av_packet_unref(avpkt);
541 
542  return ret;
543 }
544 #endif
545 
547 {
548  int i;
549 #if FF_API_CODED_FRAME
551  avctx->coded_frame = av_frame_alloc();
552  if (!avctx->coded_frame) {
553  return AVERROR(ENOMEM);
554  }
556 #endif
557 
558  if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
559  av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
560  return AVERROR(EINVAL);
561  }
562 
563  if (avctx->codec->sample_fmts) {
564  for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
565  if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
566  break;
567  if (avctx->channels == 1 &&
570  avctx->sample_fmt = avctx->codec->sample_fmts[i];
571  break;
572  }
573  }
574  if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
575  char buf[128];
576  snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
577  av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
578  (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
579  return AVERROR(EINVAL);
580  }
581  }
582  if (avctx->codec->pix_fmts) {
583  for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
584  if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
585  break;
586  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
587  && !(avctx->codec_id == AV_CODEC_ID_MJPEG
589  char buf[128];
590  snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
591  av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
592  (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
593  return AVERROR(EINVAL);
594  }
595  if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
596  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
597  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
598  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
599  avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
600  avctx->color_range = AVCOL_RANGE_JPEG;
601  }
602  if (avctx->codec->supported_samplerates) {
603  for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
604  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
605  break;
606  if (avctx->codec->supported_samplerates[i] == 0) {
607  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
608  avctx->sample_rate);
609  return AVERROR(EINVAL);
610  }
611  }
612  if (avctx->sample_rate < 0) {
613  av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
614  avctx->sample_rate);
615  return AVERROR(EINVAL);
616  }
617  if (avctx->codec->channel_layouts) {
618  if (!avctx->channel_layout) {
619  av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
620  } else {
621  for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
622  if (avctx->channel_layout == avctx->codec->channel_layouts[i])
623  break;
624  if (avctx->codec->channel_layouts[i] == 0) {
625  char buf[512];
626  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
627  av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
628  return AVERROR(EINVAL);
629  }
630  }
631  }
632  if (avctx->channel_layout && avctx->channels) {
634  if (channels != avctx->channels) {
635  char buf[512];
636  av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
637  av_log(avctx, AV_LOG_ERROR,
638  "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
639  buf, channels, avctx->channels);
640  return AVERROR(EINVAL);
641  }
642  } else if (avctx->channel_layout) {
644  }
645  if (avctx->channels < 0) {
646  av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
647  avctx->channels);
648  return AVERROR(EINVAL);
649  }
650  if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
651  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
652  if ( avctx->bits_per_raw_sample < 0
653  || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
654  av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
655  avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
656  avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
657  }
658  if (avctx->width <= 0 || avctx->height <= 0) {
659  av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
660  return AVERROR(EINVAL);
661  }
662  }
663  if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
664  && avctx->bit_rate>0 && avctx->bit_rate<1000) {
665  av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
666  }
667 
668  if (!avctx->rc_initial_buffer_occupancy)
669  avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
670 
671  if (avctx->ticks_per_frame && avctx->time_base.num &&
672  avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
673  av_log(avctx, AV_LOG_ERROR,
674  "ticks_per_frame %d too large for the timebase %d/%d.",
675  avctx->ticks_per_frame,
676  avctx->time_base.num,
677  avctx->time_base.den);
678  return AVERROR(EINVAL);
679  }
680 
681  if (avctx->hw_frames_ctx) {
682  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
683  if (frames_ctx->format != avctx->pix_fmt) {
684  av_log(avctx, AV_LOG_ERROR,
685  "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
686  return AVERROR(EINVAL);
687  }
688  if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
689  avctx->sw_pix_fmt != frames_ctx->sw_format) {
690  av_log(avctx, AV_LOG_ERROR,
691  "Mismatching AVCodecContext.sw_pix_fmt (%s) "
692  "and AVHWFramesContext.sw_format (%s)\n",
694  av_get_pix_fmt_name(frames_ctx->sw_format));
695  return AVERROR(EINVAL);
696  }
697  avctx->sw_pix_fmt = frames_ctx->sw_format;
698  }
699 
700  return 0;
701 }
702 
703 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
704 {
705  uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
706  const char *names[] = {"Intra", "Inter", "Chroma Intra"};
707  for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
708  uint16_t *matrix = matrices[m];
709  if (matrix && (types & (1U << m))) {
710  for (int i = 0; i < 64; i++) {
711  if (matrix[i] < min || matrix[i] > max) {
712  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);
713  return AVERROR(EINVAL);
714  }
715  }
716  }
717  }
718  return 0;
719 }
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:1609
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1788
void av_init_packet(AVPacket *pkt)
Definition: avpacket.c:36
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define CONFIG_FRAME_THREAD_ENCODER
Definition: config.h:643
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define max(a, b)
Definition: cuda_runtime.h:33
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:162
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
Definition: encode.c:319
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:34
int ff_encode_preinit(AVCodecContext *avctx)
Definition: encode.c:546
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:285
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:272
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition: encode.c:177
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition: encode.c:703
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
Pad last frame with silence.
Definition: encode.c:116
static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt, int *got_packet, const AVFrame *frame)
Definition: encode.c:436
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
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition: codec.h:116
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:414
#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:383
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
The default callback for AVCodecContext.get_encode_buffer().
Definition: encode.c:60
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:532
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition: encode.c:147
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:519
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:854
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:291
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:220
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:244
#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:277
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 * desc
Definition: libsvtav1.c:83
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
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
@ 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 FF_ARRAY_ELEMS(a)
#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
uint16_t * chroma_intra_matrix
custom intra quantization matrix
Definition: avcodec.h:2164
int width
picture width / height.
Definition: avcodec.h:709
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1561
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1405
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:2082
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:2252
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:1605
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:2222
enum AVMediaType codec_type
Definition: avcodec.h:544
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1768
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:668
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:1045
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1796
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:2388
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:1448
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int sample_rate
samples per second
Definition: avcodec.h:1196
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1227
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:1036
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:166
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
Definition: internal.h:165
EncodeSimpleContext es
Definition: internal.h:170
int last_audio_frame
An audio frame with less than required samples has been submitted and padded with silence.
Definition: internal.h:142
AVFrame * buffer_frame
Definition: internal.h:191
AVPacket * buffer_pkt
buffers for using new encode/decode API through legacy API
Definition: internal.h:190
int draining
checks API usage: after codec draining, flush is required to resume operation
Definition: internal.h:185
void * frame_thread_encoder
Definition: internal.h:168
AVPacket * compat_encode_packet
Definition: internal.h:203
int compat_decode_warned
Definition: internal.h:195
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
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
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:126
#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
float min