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"
29 #include "frame_thread_encoder.h"
32 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
34 if (avpkt->size < 0) {
35 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
36 return AVERROR(EINVAL);
38 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
39 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
40 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
41 return AVERROR(EINVAL);
44 if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
45 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
46 if (!avpkt->data || avpkt->size < size) {
47 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
48 avpkt->data = avctx->internal->byte_buffer;
49 avpkt->size = avctx->internal->byte_buffer_size;
54 AVBufferRef *buf = avpkt->buf;
56 if (avpkt->size < size) {
57 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
58 return AVERROR(EINVAL);
61 av_init_packet(avpkt);
66 int ret = av_new_packet(avpkt, size);
68 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
74 * Pad last frame with silence.
76 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
78 AVFrame *frame = NULL;
81 if (!(frame = av_frame_alloc()))
82 return AVERROR(ENOMEM);
84 frame->format = src->format;
85 frame->channel_layout = src->channel_layout;
86 frame->channels = src->channels;
87 frame->nb_samples = s->frame_size;
88 ret = av_frame_get_buffer(frame, 0);
92 ret = av_frame_copy_props(frame, src);
96 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
97 src->nb_samples, s->channels, s->sample_fmt)) < 0)
99 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
100 frame->nb_samples - src->nb_samples,
101 s->channels, s->sample_fmt)) < 0)
109 av_frame_free(&frame);
113 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
115 const AVFrame *frame,
118 AVFrame *extended_frame = NULL;
119 AVFrame *padded_frame = NULL;
121 AVPacket user_pkt = *avpkt;
122 int needs_realloc = !user_pkt.data;
126 if (!avctx->codec->encode2) {
127 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
128 return AVERROR(ENOSYS);
131 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
132 av_packet_unref(avpkt);
136 /* ensure that extended_data is properly set */
137 if (frame && !frame->extended_data) {
138 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
139 avctx->channels > AV_NUM_DATA_POINTERS) {
140 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
141 "with more than %d channels, but extended_data is not set.\n",
142 AV_NUM_DATA_POINTERS);
143 return AVERROR(EINVAL);
145 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
147 extended_frame = av_frame_alloc();
149 return AVERROR(ENOMEM);
151 memcpy(extended_frame, frame, sizeof(AVFrame));
152 extended_frame->extended_data = extended_frame->data;
153 frame = extended_frame;
156 /* extract audio service type metadata */
158 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
159 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
160 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
163 /* check for valid frame size */
165 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
166 if (frame->nb_samples > avctx->frame_size) {
167 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
168 ret = AVERROR(EINVAL);
171 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
172 /* if we already got an undersized frame, that must have been the last */
173 if (avctx->internal->last_audio_frame) {
174 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size);
175 ret = AVERROR(EINVAL);
179 if (frame->nb_samples < avctx->frame_size) {
180 ret = pad_last_frame(avctx, &padded_frame, frame);
184 frame = padded_frame;
185 avctx->internal->last_audio_frame = 1;
188 if (frame->nb_samples != avctx->frame_size) {
189 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
190 ret = AVERROR(EINVAL);
196 av_assert0(avctx->codec->encode2);
198 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
200 if (*got_packet_ptr) {
201 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
202 if (avpkt->pts == AV_NOPTS_VALUE)
203 avpkt->pts = frame->pts;
204 if (!avpkt->duration)
205 avpkt->duration = ff_samples_to_time_base(avctx,
208 avpkt->dts = avpkt->pts;
213 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
216 if (user_pkt.size >= avpkt->size) {
217 memcpy(user_pkt.data, avpkt->data, avpkt->size);
219 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
220 avpkt->size = user_pkt.size;
223 avpkt->buf = user_pkt.buf;
224 avpkt->data = user_pkt.data;
225 } else if (!avpkt->buf) {
226 ret = av_packet_make_refcounted(avpkt);
233 if (needs_realloc && avpkt->data) {
234 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
236 avpkt->data = avpkt->buf->data;
239 avctx->frame_number++;
242 if (ret < 0 || !*got_packet_ptr) {
243 av_packet_unref(avpkt);
247 /* NOTE: if we add any audio encoders which output non-keyframe packets,
248 * this needs to be moved to the encoders, but for now we can do it
249 * here to simplify things */
250 avpkt->flags |= AV_PKT_FLAG_KEY;
253 av_frame_free(&padded_frame);
254 av_free(extended_frame);
259 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
261 const AVFrame *frame,
265 AVPacket user_pkt = *avpkt;
266 int needs_realloc = !user_pkt.data;
270 if (!avctx->codec->encode2) {
271 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
272 return AVERROR(ENOSYS);
275 if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
276 avctx->stats_out[0] = '\0';
279 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
280 (avctx->internal->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME))) {
281 av_packet_unref(avpkt);
285 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
286 return AVERROR(EINVAL);
288 if (frame && frame->format == AV_PIX_FMT_NONE)
289 av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
290 if (frame && (frame->width == 0 || frame->height == 0))
291 av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
293 av_assert0(avctx->codec->encode2);
296 if (CONFIG_FRAME_THREAD_ENCODER &&
297 avctx->internal->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
298 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
300 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
301 if (*got_packet_ptr && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
302 avpkt->pts = avpkt->dts = frame->pts;
304 av_assert0(ret <= 0);
308 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
311 if (user_pkt.size >= avpkt->size) {
312 memcpy(user_pkt.data, avpkt->data, avpkt->size);
314 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
315 avpkt->size = user_pkt.size;
318 avpkt->buf = user_pkt.buf;
319 avpkt->data = user_pkt.data;
320 } else if (!avpkt->buf) {
321 ret = av_packet_make_refcounted(avpkt);
328 if (!*got_packet_ptr)
331 if (needs_realloc && avpkt->data) {
332 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
334 avpkt->data = avpkt->buf->data;
338 avctx->frame_number++;
341 if (ret < 0 || !*got_packet_ptr)
342 av_packet_unref(avpkt);
347 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
348 const AVSubtitle *sub)
351 if (sub->start_display_time) {
352 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
356 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
357 avctx->frame_number++;
361 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
366 av_packet_unref(avctx->internal->buffer_pkt);
367 avctx->internal->buffer_pkt_valid = 0;
369 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
370 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
372 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
373 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
376 ret = AVERROR(EINVAL);
379 if (ret >= 0 && *got_packet) {
380 // Encoders must always return ref-counted buffers.
381 // Side-data only packets have no data and can be not ref-counted.
382 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
383 avctx->internal->buffer_pkt_valid = 1;
386 av_packet_unref(avctx->internal->buffer_pkt);
392 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
394 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
395 return AVERROR(EINVAL);
397 if (avctx->internal->draining)
401 avctx->internal->draining = 1;
403 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
407 if (avctx->codec->send_frame)
408 return avctx->codec->send_frame(avctx, frame);
410 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
411 // 1. if the AVFrame is not refcounted, the copying will be much more
412 // expensive than copying the packet data
413 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
416 if (avctx->internal->buffer_pkt_valid)
417 return AVERROR(EAGAIN);
419 return do_encode(avctx, frame, &(int){0});
422 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
424 av_packet_unref(avpkt);
426 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
427 return AVERROR(EINVAL);
429 if (avctx->codec->receive_packet) {
431 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
433 ret = avctx->codec->receive_packet(avctx, avpkt);
435 // Encoders must always return ref-counted buffers.
436 // Side-data only packets have no data and can be not ref-counted.
437 av_assert0(!avpkt->data || avpkt->buf);
441 // Emulation via old API.
443 if (!avctx->internal->buffer_pkt_valid) {
446 if (!avctx->internal->draining)
447 return AVERROR(EAGAIN);
448 ret = do_encode(avctx, NULL, &got_packet);
451 if (ret >= 0 && !got_packet)
455 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
456 avctx->internal->buffer_pkt_valid = 0;