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);
73 int ff_alloc_packet(AVPacket *avpkt, int size)
75 return ff_alloc_packet2(NULL, avpkt, size, 0);
79 * Pad last frame with silence.
81 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
83 AVFrame *frame = NULL;
86 if (!(frame = av_frame_alloc()))
87 return AVERROR(ENOMEM);
89 frame->format = src->format;
90 frame->channel_layout = src->channel_layout;
91 frame->channels = src->channels;
92 frame->nb_samples = s->frame_size;
93 ret = av_frame_get_buffer(frame, 0);
97 ret = av_frame_copy_props(frame, src);
101 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
102 src->nb_samples, s->channels, s->sample_fmt)) < 0)
104 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
105 frame->nb_samples - src->nb_samples,
106 s->channels, s->sample_fmt)) < 0)
114 av_frame_free(&frame);
118 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
120 const AVFrame *frame,
123 AVFrame *extended_frame = NULL;
124 AVFrame *padded_frame = NULL;
126 AVPacket user_pkt = *avpkt;
127 int needs_realloc = !user_pkt.data;
131 if (!avctx->codec->encode2) {
132 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
133 return AVERROR(ENOSYS);
136 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
137 av_packet_unref(avpkt);
141 /* ensure that extended_data is properly set */
142 if (frame && !frame->extended_data) {
143 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
144 avctx->channels > AV_NUM_DATA_POINTERS) {
145 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
146 "with more than %d channels, but extended_data is not set.\n",
147 AV_NUM_DATA_POINTERS);
148 return AVERROR(EINVAL);
150 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
152 extended_frame = av_frame_alloc();
154 return AVERROR(ENOMEM);
156 memcpy(extended_frame, frame, sizeof(AVFrame));
157 extended_frame->extended_data = extended_frame->data;
158 frame = extended_frame;
161 /* extract audio service type metadata */
163 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
164 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
165 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
168 /* check for valid frame size */
170 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
171 if (frame->nb_samples > avctx->frame_size) {
172 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
173 ret = AVERROR(EINVAL);
176 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
177 /* if we already got an undersized frame, that must have been the last */
178 if (avctx->internal->last_audio_frame) {
179 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size);
180 ret = AVERROR(EINVAL);
184 if (frame->nb_samples < avctx->frame_size) {
185 ret = pad_last_frame(avctx, &padded_frame, frame);
189 frame = padded_frame;
190 avctx->internal->last_audio_frame = 1;
193 if (frame->nb_samples != avctx->frame_size) {
194 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
195 ret = AVERROR(EINVAL);
201 av_assert0(avctx->codec->encode2);
203 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
205 if (*got_packet_ptr) {
206 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
207 if (avpkt->pts == AV_NOPTS_VALUE)
208 avpkt->pts = frame->pts;
209 if (!avpkt->duration)
210 avpkt->duration = ff_samples_to_time_base(avctx,
213 avpkt->dts = avpkt->pts;
218 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
221 if (user_pkt.size >= avpkt->size) {
222 memcpy(user_pkt.data, avpkt->data, avpkt->size);
224 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
225 avpkt->size = user_pkt.size;
228 avpkt->buf = user_pkt.buf;
229 avpkt->data = user_pkt.data;
230 } else if (!avpkt->buf) {
231 ret = av_packet_make_refcounted(avpkt);
238 if (needs_realloc && avpkt->data) {
239 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
241 avpkt->data = avpkt->buf->data;
244 avctx->frame_number++;
247 if (ret < 0 || !*got_packet_ptr) {
248 av_packet_unref(avpkt);
252 /* NOTE: if we add any audio encoders which output non-keyframe packets,
253 * this needs to be moved to the encoders, but for now we can do it
254 * here to simplify things */
255 avpkt->flags |= AV_PKT_FLAG_KEY;
258 av_frame_free(&padded_frame);
259 av_free(extended_frame);
264 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
266 const AVFrame *frame,
270 AVPacket user_pkt = *avpkt;
271 int needs_realloc = !user_pkt.data;
275 if (!avctx->codec->encode2) {
276 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
277 return AVERROR(ENOSYS);
280 if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
281 avctx->stats_out[0] = '\0';
284 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
285 (avctx->internal->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME))) {
286 av_packet_unref(avpkt);
290 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
291 return AVERROR(EINVAL);
293 if (frame && frame->format == AV_PIX_FMT_NONE)
294 av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
295 if (frame && (frame->width == 0 || frame->height == 0))
296 av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
298 av_assert0(avctx->codec->encode2);
301 if (CONFIG_FRAME_THREAD_ENCODER &&
302 avctx->internal->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
303 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
305 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
306 if (*got_packet_ptr && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
307 avpkt->pts = avpkt->dts = frame->pts;
309 av_assert0(ret <= 0);
313 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
316 if (user_pkt.size >= avpkt->size) {
317 memcpy(user_pkt.data, avpkt->data, avpkt->size);
319 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
320 avpkt->size = user_pkt.size;
323 avpkt->buf = user_pkt.buf;
324 avpkt->data = user_pkt.data;
325 } else if (!avpkt->buf) {
326 ret = av_packet_make_refcounted(avpkt);
333 if (!*got_packet_ptr)
336 if (needs_realloc && avpkt->data) {
337 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
339 avpkt->data = avpkt->buf->data;
343 avctx->frame_number++;
346 if (ret < 0 || !*got_packet_ptr)
347 av_packet_unref(avpkt);
352 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
353 const AVSubtitle *sub)
356 if (sub->start_display_time) {
357 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
361 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
362 avctx->frame_number++;
366 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
371 av_packet_unref(avctx->internal->buffer_pkt);
372 avctx->internal->buffer_pkt_valid = 0;
374 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
375 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
377 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
378 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
381 ret = AVERROR(EINVAL);
384 if (ret >= 0 && *got_packet) {
385 // Encoders must always return ref-counted buffers.
386 // Side-data only packets have no data and can be not ref-counted.
387 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
388 avctx->internal->buffer_pkt_valid = 1;
391 av_packet_unref(avctx->internal->buffer_pkt);
397 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
399 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
400 return AVERROR(EINVAL);
402 if (avctx->internal->draining)
406 avctx->internal->draining = 1;
408 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
412 if (avctx->codec->send_frame)
413 return avctx->codec->send_frame(avctx, frame);
415 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
416 // 1. if the AVFrame is not refcounted, the copying will be much more
417 // expensive than copying the packet data
418 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
421 if (avctx->internal->buffer_pkt_valid)
422 return AVERROR(EAGAIN);
424 return do_encode(avctx, frame, &(int){0});
427 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
429 av_packet_unref(avpkt);
431 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
432 return AVERROR(EINVAL);
434 if (avctx->codec->receive_packet) {
436 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
438 ret = avctx->codec->receive_packet(avctx, avpkt);
440 // Encoders must always return ref-counted buffers.
441 // Side-data only packets have no data and can be not ref-counted.
442 av_assert0(!avpkt->data || avpkt->buf);
446 // Emulation via old API.
448 if (!avctx->internal->buffer_pkt_valid) {
451 if (!avctx->internal->draining)
452 return AVERROR(EAGAIN);
453 ret = do_encode(avctx, NULL, &got_packet);
456 if (ret >= 0 && !got_packet)
460 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
461 avctx->internal->buffer_pkt_valid = 0;