2016-10-26 08:10:19 +02:00
|
|
|
/*
|
|
|
|
|
* generic encoding-related code
|
|
|
|
|
*
|
2017-04-07 00:42:38 -03:00
|
|
|
* This file is part of FFmpeg.
|
2016-10-26 08:10:19 +02:00
|
|
|
*
|
2017-04-07 00:42:38 -03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-10-26 08:10:19 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
2017-04-07 00:42:38 -03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-10-26 08:10:19 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2017-04-07 00:42:38 -03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-10-26 08:10:19 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
|
#include "libavutil/frame.h"
|
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
|
#include "libavutil/internal.h"
|
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2020-06-09 18:31:32 -03:00
|
|
|
#include "encode.h"
|
2017-04-07 00:42:38 -03:00
|
|
|
#include "frame_thread_encoder.h"
|
2016-10-26 08:10:19 +02:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
2017-04-07 00:42:38 -03:00
|
|
|
if (avpkt->size < 0) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
}
|
|
|
|
|
if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
|
|
|
|
|
size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR(EINVAL);
|
2017-04-07 00:42:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
|
|
|
|
|
av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
|
|
|
|
|
if (!avpkt->data || avpkt->size < size) {
|
|
|
|
|
av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
|
|
|
|
|
avpkt->data = avctx->internal->byte_buffer;
|
|
|
|
|
avpkt->size = avctx->internal->byte_buffer_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
if (avpkt->data) {
|
|
|
|
|
AVBufferRef *buf = avpkt->buf;
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if (avpkt->size < size) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR(EINVAL);
|
2017-04-07 00:42:38 -03:00
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
av_init_packet(avpkt);
|
|
|
|
|
avpkt->buf = buf;
|
|
|
|
|
avpkt->size = size;
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
2017-04-07 00:42:38 -03:00
|
|
|
int ret = av_new_packet(avpkt, size);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
|
|
|
|
|
return ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pad last frame with silence.
|
|
|
|
|
*/
|
2020-06-09 18:31:32 -03:00
|
|
|
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
frame->format = src->format;
|
|
|
|
|
frame->channel_layout = src->channel_layout;
|
2017-04-22 15:54:58 +07:00
|
|
|
frame->channels = src->channels;
|
2016-10-26 08:10:19 +02:00
|
|
|
frame->nb_samples = s->frame_size;
|
2017-02-08 09:51:17 +01:00
|
|
|
ret = av_frame_get_buffer(frame, 0);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
ret = av_frame_copy_props(frame, src);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
|
|
|
|
|
src->nb_samples, s->channels, s->sample_fmt)) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
|
|
|
|
|
frame->nb_samples - src->nb_samples,
|
|
|
|
|
s->channels, s->sample_fmt)) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
2020-06-09 18:31:32 -03:00
|
|
|
av_frame_unref(frame);
|
2016-10-26 08:10:19 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
|
|
|
|
|
AVPacket *avpkt,
|
|
|
|
|
const AVFrame *frame,
|
|
|
|
|
int *got_packet_ptr)
|
|
|
|
|
{
|
2017-04-07 00:42:38 -03:00
|
|
|
AVFrame *extended_frame = NULL;
|
2016-10-26 08:10:19 +02:00
|
|
|
AVFrame *padded_frame = NULL;
|
|
|
|
|
int ret;
|
2017-04-07 00:42:38 -03:00
|
|
|
AVPacket user_pkt = *avpkt;
|
|
|
|
|
int needs_realloc = !user_pkt.data;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
*got_packet_ptr = 0;
|
|
|
|
|
|
|
|
|
|
if (!avctx->codec->encode2) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
|
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
|
|
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ensure that extended_data is properly set */
|
|
|
|
|
if (frame && !frame->extended_data) {
|
|
|
|
|
if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
|
|
|
|
|
avctx->channels > AV_NUM_DATA_POINTERS) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
|
|
|
|
|
"with more than %d channels, but extended_data is not set.\n",
|
|
|
|
|
AV_NUM_DATA_POINTERS);
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
}
|
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
extended_frame = av_frame_alloc();
|
|
|
|
|
if (!extended_frame)
|
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
|
|
memcpy(extended_frame, frame, sizeof(AVFrame));
|
|
|
|
|
extended_frame->extended_data = extended_frame->data;
|
|
|
|
|
frame = extended_frame;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* extract audio service type metadata */
|
|
|
|
|
if (frame) {
|
|
|
|
|
AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
|
|
|
|
|
if (sd && sd->size >= sizeof(enum AVAudioServiceType))
|
|
|
|
|
avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check for valid frame size */
|
|
|
|
|
if (frame) {
|
|
|
|
|
if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
|
2017-04-07 00:42:38 -03:00
|
|
|
if (frame->nb_samples > avctx->frame_size) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
|
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
} else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
|
2019-08-03 21:44:28 +02:00
|
|
|
/* if we already got an undersized frame, that must have been the last */
|
|
|
|
|
if (avctx->internal->last_audio_frame) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size);
|
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frame->nb_samples < avctx->frame_size) {
|
2020-06-09 18:31:32 -03:00
|
|
|
if (!(padded_frame = av_frame_alloc())) {
|
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
ret = pad_last_frame(avctx, padded_frame, frame);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (ret < 0)
|
2017-04-07 00:42:38 -03:00
|
|
|
goto end;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
frame = padded_frame;
|
|
|
|
|
avctx->internal->last_audio_frame = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frame->nb_samples != avctx->frame_size) {
|
2017-04-07 00:42:38 -03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
|
2016-10-26 08:10:19 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
av_assert0(avctx->codec->encode2);
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
if (*got_packet_ptr) {
|
|
|
|
|
if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
|
|
|
|
|
if (avpkt->pts == AV_NOPTS_VALUE)
|
|
|
|
|
avpkt->pts = frame->pts;
|
|
|
|
|
if (!avpkt->duration)
|
|
|
|
|
avpkt->duration = ff_samples_to_time_base(avctx,
|
|
|
|
|
frame->nb_samples);
|
|
|
|
|
}
|
|
|
|
|
avpkt->dts = avpkt->pts;
|
|
|
|
|
} else {
|
|
|
|
|
avpkt->size = 0;
|
|
|
|
|
}
|
2017-04-07 00:42:38 -03:00
|
|
|
}
|
|
|
|
|
if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
|
|
|
|
|
needs_realloc = 0;
|
|
|
|
|
if (user_pkt.data) {
|
|
|
|
|
if (user_pkt.size >= avpkt->size) {
|
|
|
|
|
memcpy(user_pkt.data, avpkt->data, avpkt->size);
|
|
|
|
|
} else {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
|
|
|
|
|
avpkt->size = user_pkt.size;
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
avpkt->buf = user_pkt.buf;
|
|
|
|
|
avpkt->data = user_pkt.data;
|
2017-09-26 00:24:29 -03:00
|
|
|
} else if (!avpkt->buf) {
|
2018-03-24 21:58:56 -03:00
|
|
|
ret = av_packet_make_refcounted(avpkt);
|
2017-10-03 11:49:18 -03:00
|
|
|
if (ret < 0)
|
|
|
|
|
goto end;
|
2017-04-07 00:42:38 -03:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if (!ret) {
|
|
|
|
|
if (needs_realloc && avpkt->data) {
|
|
|
|
|
ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
|
avpkt->data = avpkt->buf->data;
|
|
|
|
|
}
|
2018-06-28 17:01:46 +08:00
|
|
|
if (frame)
|
|
|
|
|
avctx->frame_number++;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret < 0 || !*got_packet_ptr) {
|
|
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* NOTE: if we add any audio encoders which output non-keyframe packets,
|
|
|
|
|
* this needs to be moved to the encoders, but for now we can do it
|
|
|
|
|
* here to simplify things */
|
|
|
|
|
avpkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
av_frame_free(&padded_frame);
|
2017-04-07 00:42:38 -03:00
|
|
|
av_free(extended_frame);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
|
|
|
|
|
AVPacket *avpkt,
|
|
|
|
|
const AVFrame *frame,
|
|
|
|
|
int *got_packet_ptr)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
2017-04-07 00:42:38 -03:00
|
|
|
AVPacket user_pkt = *avpkt;
|
|
|
|
|
int needs_realloc = !user_pkt.data;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
|
*got_packet_ptr = 0;
|
|
|
|
|
|
|
|
|
|
if (!avctx->codec->encode2) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
|
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
|
|
|
|
|
avctx->stats_out[0] = '\0';
|
|
|
|
|
|
2020-05-15 20:24:19 -03:00
|
|
|
if (!frame &&
|
|
|
|
|
!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
|
|
|
|
|
(avctx->internal->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME))) {
|
2016-10-26 08:10:19 +02:00
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if (frame && frame->format == AV_PIX_FMT_NONE)
|
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
|
|
|
|
|
if (frame && (frame->width == 0 || frame->height == 0))
|
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
av_assert0(avctx->codec->encode2);
|
|
|
|
|
|
2020-05-15 20:24:19 -03:00
|
|
|
|
|
|
|
|
if (CONFIG_FRAME_THREAD_ENCODER &&
|
|
|
|
|
avctx->internal->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
|
|
|
|
|
ret = ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
|
|
|
|
|
else {
|
|
|
|
|
ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
|
|
|
|
|
if (*got_packet_ptr && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
|
|
|
|
|
avpkt->pts = avpkt->dts = frame->pts;
|
|
|
|
|
}
|
2017-04-07 00:42:38 -03:00
|
|
|
av_assert0(ret <= 0);
|
|
|
|
|
|
|
|
|
|
emms_c();
|
|
|
|
|
|
|
|
|
|
if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
|
|
|
|
|
needs_realloc = 0;
|
|
|
|
|
if (user_pkt.data) {
|
|
|
|
|
if (user_pkt.size >= avpkt->size) {
|
|
|
|
|
memcpy(user_pkt.data, avpkt->data, avpkt->size);
|
|
|
|
|
} else {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
|
|
|
|
|
avpkt->size = user_pkt.size;
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
avpkt->buf = user_pkt.buf;
|
|
|
|
|
avpkt->data = user_pkt.data;
|
2017-09-26 00:24:29 -03:00
|
|
|
} else if (!avpkt->buf) {
|
2018-03-24 21:58:56 -03:00
|
|
|
ret = av_packet_make_refcounted(avpkt);
|
2017-09-26 00:24:29 -03:00
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
2017-04-07 00:42:38 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!ret) {
|
|
|
|
|
if (!*got_packet_ptr)
|
|
|
|
|
avpkt->size = 0;
|
|
|
|
|
|
2017-04-07 00:42:38 -03:00
|
|
|
if (needs_realloc && avpkt->data) {
|
|
|
|
|
ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
|
avpkt->data = avpkt->buf->data;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 17:01:46 +08:00
|
|
|
if (frame)
|
|
|
|
|
avctx->frame_number++;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret < 0 || !*got_packet_ptr)
|
|
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
|
|
|
|
|
const AVSubtitle *sub)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
if (sub->start_display_time) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-04-07 00:42:38 -03:00
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
|
|
|
|
|
avctx->frame_number++;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
|
{
|
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
|
|
|
|
|
if (avci->draining)
|
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
|
|
if (!avci->buffer_frame->buf[0])
|
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
|
|
av_frame_move_ref(frame, avci->buffer_frame);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
2020-06-09 18:31:32 -03:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
EncodeSimpleContext *es = &avci->es;
|
|
|
|
|
AVFrame *frame = es->in_frame;
|
|
|
|
|
int got_packet;
|
2016-10-26 08:10:19 +02:00
|
|
|
int ret;
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (avci->draining_done)
|
|
|
|
|
return AVERROR_EOF;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (!frame->buf[0] && !avci->draining) {
|
|
|
|
|
av_frame_unref(frame);
|
|
|
|
|
ret = ff_encode_get_frame(avctx, frame);
|
|
|
|
|
if (ret < 0 && ret != AVERROR_EOF)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!frame->buf[0]) {
|
|
|
|
|
if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
|
|
|
|
|
(avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
|
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
|
|
// Flushing is signaled with a NULL frame
|
|
|
|
|
frame = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got_packet = 0;
|
|
|
|
|
|
|
|
|
|
av_assert0(avctx->codec->encode2);
|
|
|
|
|
|
|
|
|
|
if (CONFIG_FRAME_THREAD_ENCODER &&
|
|
|
|
|
avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
|
|
|
|
|
ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
|
|
|
|
|
else {
|
|
|
|
|
ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
|
|
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
|
|
|
|
|
!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
|
|
|
|
|
avpkt->pts = avpkt->dts = frame->pts;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
av_assert0(ret <= 0);
|
|
|
|
|
|
|
|
|
|
emms_c();
|
|
|
|
|
|
|
|
|
|
if (!ret && got_packet) {
|
|
|
|
|
if (avpkt->data) {
|
|
|
|
|
ret = av_packet_make_refcounted(avpkt);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
|
|
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
|
if (avpkt->pts == AV_NOPTS_VALUE)
|
|
|
|
|
avpkt->pts = frame->pts;
|
|
|
|
|
if (!avpkt->duration)
|
|
|
|
|
avpkt->duration = ff_samples_to_time_base(avctx,
|
|
|
|
|
frame->nb_samples);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
|
/* NOTE: if we add any audio encoders which output non-keyframe packets,
|
|
|
|
|
* this needs to be moved to the encoders, but for now we can do it
|
|
|
|
|
* here to simplify things */
|
|
|
|
|
avpkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
avpkt->dts = avpkt->pts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (avci->draining && !got_packet)
|
|
|
|
|
avci->draining_done = 1;
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (ret < 0 || !got_packet)
|
|
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
|
if (!ret)
|
|
|
|
|
avctx->frame_number++;
|
|
|
|
|
av_frame_unref(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (got_packet)
|
2016-10-26 08:10:19 +02:00
|
|
|
// Encoders must always return ref-counted buffers.
|
|
|
|
|
// Side-data only packets have no data and can be not ref-counted.
|
2020-06-09 18:31:32 -03:00
|
|
|
av_assert0(!avpkt->data || avpkt->buf);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
while (!avpkt->data && !avpkt->side_data) {
|
|
|
|
|
ret = encode_simple_internal(avctx, avpkt);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
|
|
|
|
|
{
|
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (avci->draining_done)
|
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
|
|
av_assert0(!avpkt->data && !avpkt->side_data);
|
|
|
|
|
|
|
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
|
if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
|
|
|
|
|
avctx->stats_out[0] = '\0';
|
|
|
|
|
if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
|
|
|
|
|
return AVERROR(EINVAL);
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (avctx->codec->receive_packet) {
|
|
|
|
|
ret = avctx->codec->receive_packet(avctx, avpkt);
|
|
|
|
|
if (!ret)
|
|
|
|
|
// Encoders must always return ref-counted buffers.
|
|
|
|
|
// Side-data only packets have no data and can be not ref-counted.
|
|
|
|
|
av_assert0(!avpkt->data || avpkt->buf);
|
|
|
|
|
} else
|
|
|
|
|
ret = encode_simple_receive_packet(avctx, avpkt);
|
|
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
|
avci->draining_done = 1;
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
|
|
|
|
|
{
|
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
AVFrame *dst = avci->buffer_frame;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
|
/* extract audio service type metadata */
|
|
|
|
|
AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
|
|
|
|
|
if (sd && sd->size >= sizeof(enum AVAudioServiceType))
|
|
|
|
|
avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
|
|
|
|
|
|
|
|
|
|
/* check for valid frame size */
|
|
|
|
|
if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
|
|
|
|
|
if (src->nb_samples > avctx->frame_size) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
}
|
|
|
|
|
} else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
|
|
|
|
|
/* if we already got an undersized frame, that must have been the last */
|
|
|
|
|
if (avctx->internal->last_audio_frame) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (src->nb_samples < avctx->frame_size) {
|
|
|
|
|
ret = pad_last_frame(avctx, dst, src);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
avctx->internal->last_audio_frame = 1;
|
|
|
|
|
} else if (src->nb_samples > avctx->frame_size) {
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dst->data[0]) {
|
|
|
|
|
ret = av_frame_ref(dst, src);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
|
|
|
|
|
{
|
2020-06-09 18:31:32 -03:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (avci->draining)
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (avci->buffer_frame->data[0])
|
|
|
|
|
return AVERROR(EAGAIN);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (!frame) {
|
|
|
|
|
avci->draining = 1;
|
|
|
|
|
} else {
|
|
|
|
|
ret = encode_send_frame_internal(avctx, frame);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
|
|
|
|
|
ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
|
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
return 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
|
|
|
|
|
{
|
2020-06-09 18:31:32 -03:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
av_packet_unref(avpkt);
|
|
|
|
|
|
|
|
|
|
if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
|
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
2020-06-09 18:31:32 -03:00
|
|
|
if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
|
|
|
|
|
av_packet_move_ref(avpkt, avci->buffer_pkt);
|
|
|
|
|
} else {
|
|
|
|
|
ret = encode_receive_packet_internal(avctx, avpkt);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|