2 * generic encoding-related code
4 * This file is part of FFmpeg.
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.
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.
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
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"
30 #include "frame_thread_encoder.h"
33 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
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",
37 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
38 return AVERROR(EINVAL);
41 av_assert0(!avpkt->data);
43 if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
44 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
45 avpkt->data = avctx->internal->byte_buffer;
50 int ret = av_new_packet(avpkt, size);
52 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
59 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
63 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
64 return AVERROR(EINVAL);
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);
71 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
73 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
76 avpkt->data = avpkt->buf->data;
81 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
85 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
86 return AVERROR(EINVAL);
88 av_assert0(!avpkt->data && !avpkt->buf);
91 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
95 if (!avpkt->data || !avpkt->buf) {
96 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
97 ret = AVERROR(EINVAL);
100 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
105 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
106 av_packet_unref(avpkt);
113 * Pad last frame with silence.
115 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
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);
127 ret = av_frame_copy_props(frame, src);
131 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
132 src->nb_samples, s->channels, s->sample_fmt)) < 0)
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)
142 av_frame_unref(frame);
146 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
147 const AVSubtitle *sub)
150 if (sub->start_display_time) {
151 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
155 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
156 avctx->frame_number++;
160 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
162 AVCodecInternal *avci = avctx->internal;
167 if (!avci->buffer_frame->buf[0])
168 return AVERROR(EAGAIN);
170 av_frame_move_ref(frame, avci->buffer_frame);
175 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
177 AVCodecInternal *avci = avctx->internal;
178 EncodeSimpleContext *es = &avci->es;
179 AVFrame *frame = es->in_frame;
183 if (avci->draining_done)
186 if (!frame->buf[0] && !avci->draining) {
187 av_frame_unref(frame);
188 ret = ff_encode_get_frame(avctx, frame);
189 if (ret < 0 && ret != AVERROR_EOF)
193 if (!frame->buf[0]) {
194 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
195 (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
198 // Flushing is signaled with a NULL frame
204 av_assert0(avctx->codec->encode2);
206 if (CONFIG_FRAME_THREAD_ENCODER &&
207 avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
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);
214 ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
215 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
216 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
217 avpkt->pts = avpkt->dts = frame->pts;
220 av_assert0(ret <= 0);
224 if (!ret && got_packet) {
226 ret = av_packet_make_refcounted(avpkt);
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,
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;
249 if (avci->draining && !got_packet)
250 avci->draining_done = 1;
253 if (ret < 0 || !got_packet)
254 av_packet_unref(avpkt);
258 avctx->frame_number++;
259 av_frame_unref(frame);
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);
270 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
274 while (!avpkt->data && !avpkt->side_data) {
275 ret = encode_simple_internal(avctx, avpkt);
283 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
285 AVCodecInternal *avci = avctx->internal;
288 if (avci->draining_done)
291 av_assert0(!avpkt->data && !avpkt->side_data);
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);
300 if (avctx->codec->receive_packet) {
301 ret = avctx->codec->receive_packet(avctx, avpkt);
303 av_packet_unref(avpkt);
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);
309 ret = encode_simple_receive_packet(avctx, avpkt);
311 if (ret == AVERROR_EOF)
312 avci->draining_done = 1;
317 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
319 AVCodecInternal *avci = avctx->internal;
320 AVFrame *dst = avci->buffer_frame;
323 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
324 /* extract audio service type metadata */
325 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
326 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
327 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
329 /* check for valid frame size */
330 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
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);
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);
342 if (src->nb_samples < avctx->frame_size) {
343 ret = pad_last_frame(avctx, dst, src);
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);
356 ret = av_frame_ref(dst, src);
364 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
366 AVCodecInternal *avci = avctx->internal;
369 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
370 return AVERROR(EINVAL);
375 if (avci->buffer_frame->data[0])
376 return AVERROR(EAGAIN);
381 ret = encode_send_frame_internal(avctx, frame);
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)
395 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
397 AVCodecInternal *avci = avctx->internal;
400 av_packet_unref(avpkt);
402 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
403 return AVERROR(EINVAL);
405 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
406 av_packet_move_ref(avpkt, avci->buffer_pkt);
408 ret = encode_receive_packet_internal(avctx, avpkt);
416 int ff_encode_preinit(AVCodecContext *avctx)
420 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
421 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
422 return AVERROR(EINVAL);
425 if (avctx->codec->sample_fmts) {
426 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
427 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
429 if (avctx->channels == 1 &&
430 av_get_planar_sample_fmt(avctx->sample_fmt) ==
431 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
432 avctx->sample_fmt = avctx->codec->sample_fmts[i];
436 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
438 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
439 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
440 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
441 return AVERROR(EINVAL);
444 if (avctx->codec->pix_fmts) {
445 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
446 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
448 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
450 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
451 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
452 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
453 return AVERROR(EINVAL);
455 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
456 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
457 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
458 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
459 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
460 avctx->color_range = AVCOL_RANGE_JPEG;
462 if (avctx->codec->supported_samplerates) {
463 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
464 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
466 if (avctx->codec->supported_samplerates[i] == 0) {
467 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
469 return AVERROR(EINVAL);
472 if (avctx->sample_rate < 0) {
473 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
475 return AVERROR(EINVAL);
477 if (avctx->codec->channel_layouts) {
478 if (!avctx->channel_layout) {
479 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
481 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
482 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
484 if (avctx->codec->channel_layouts[i] == 0) {
486 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
487 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
488 return AVERROR(EINVAL);
492 if (avctx->channel_layout && avctx->channels) {
493 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
494 if (channels != avctx->channels) {
496 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
497 av_log(avctx, AV_LOG_ERROR,
498 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
499 buf, channels, avctx->channels);
500 return AVERROR(EINVAL);
502 } else if (avctx->channel_layout) {
503 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
505 if (avctx->channels < 0) {
506 av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
508 return AVERROR(EINVAL);
510 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
511 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
512 if ( avctx->bits_per_raw_sample < 0
513 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
514 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
515 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
516 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
518 if (avctx->width <= 0 || avctx->height <= 0) {
519 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
520 return AVERROR(EINVAL);
523 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
524 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
525 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
528 if (!avctx->rc_initial_buffer_occupancy)
529 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
531 if (avctx->ticks_per_frame && avctx->time_base.num &&
532 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
533 av_log(avctx, AV_LOG_ERROR,
534 "ticks_per_frame %d too large for the timebase %d/%d.",
535 avctx->ticks_per_frame,
536 avctx->time_base.num,
537 avctx->time_base.den);
538 return AVERROR(EINVAL);
541 if (avctx->hw_frames_ctx) {
542 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
543 if (frames_ctx->format != avctx->pix_fmt) {
544 av_log(avctx, AV_LOG_ERROR,
545 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
546 return AVERROR(EINVAL);
548 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
549 avctx->sw_pix_fmt != frames_ctx->sw_format) {
550 av_log(avctx, AV_LOG_ERROR,
551 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
552 "and AVHWFramesContext.sw_format (%s)\n",
553 av_get_pix_fmt_name(avctx->sw_pix_fmt),
554 av_get_pix_fmt_name(frames_ctx->sw_format));
555 return AVERROR(EINVAL);
557 avctx->sw_pix_fmt = frames_ctx->sw_format;