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);
60 * Pad last frame with silence.
62 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
66 frame->format = src->format;
67 frame->channel_layout = src->channel_layout;
68 frame->channels = src->channels;
69 frame->nb_samples = s->frame_size;
70 ret = av_frame_get_buffer(frame, 0);
74 ret = av_frame_copy_props(frame, src);
78 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
79 src->nb_samples, s->channels, s->sample_fmt)) < 0)
81 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
82 frame->nb_samples - src->nb_samples,
83 s->channels, s->sample_fmt)) < 0)
89 av_frame_unref(frame);
93 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
94 const AVSubtitle *sub)
97 if (sub->start_display_time) {
98 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
102 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
103 avctx->frame_number++;
107 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
109 AVCodecInternal *avci = avctx->internal;
114 if (!avci->buffer_frame->buf[0])
115 return AVERROR(EAGAIN);
117 av_frame_move_ref(frame, avci->buffer_frame);
122 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
124 AVCodecInternal *avci = avctx->internal;
125 EncodeSimpleContext *es = &avci->es;
126 AVFrame *frame = es->in_frame;
130 if (avci->draining_done)
133 if (!frame->buf[0] && !avci->draining) {
134 av_frame_unref(frame);
135 ret = ff_encode_get_frame(avctx, frame);
136 if (ret < 0 && ret != AVERROR_EOF)
140 if (!frame->buf[0]) {
141 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
142 (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
145 // Flushing is signaled with a NULL frame
151 av_assert0(avctx->codec->encode2);
153 if (CONFIG_FRAME_THREAD_ENCODER &&
154 avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
155 /* This might modify frame, but it doesn't matter, because
156 * the frame properties used below are not used for video
157 * (due to the delay inherent in frame threaded encoding, it makes
158 * no sense to use the properties of the current frame anyway). */
159 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
161 ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
162 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
163 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
164 avpkt->pts = avpkt->dts = frame->pts;
167 av_assert0(ret <= 0);
171 if (!ret && got_packet) {
173 ret = av_packet_make_refcounted(avpkt);
178 if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
179 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
180 if (avpkt->pts == AV_NOPTS_VALUE)
181 avpkt->pts = frame->pts;
182 if (!avpkt->duration)
183 avpkt->duration = ff_samples_to_time_base(avctx,
187 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
188 /* NOTE: if we add any audio encoders which output non-keyframe packets,
189 * this needs to be moved to the encoders, but for now we can do it
190 * here to simplify things */
191 avpkt->flags |= AV_PKT_FLAG_KEY;
192 avpkt->dts = avpkt->pts;
196 if (avci->draining && !got_packet)
197 avci->draining_done = 1;
200 if (ret < 0 || !got_packet)
201 av_packet_unref(avpkt);
205 avctx->frame_number++;
206 av_frame_unref(frame);
210 // Encoders must always return ref-counted buffers.
211 // Side-data only packets have no data and can be not ref-counted.
212 av_assert0(!avpkt->data || avpkt->buf);
217 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
221 while (!avpkt->data && !avpkt->side_data) {
222 ret = encode_simple_internal(avctx, avpkt);
230 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
232 AVCodecInternal *avci = avctx->internal;
235 if (avci->draining_done)
238 av_assert0(!avpkt->data && !avpkt->side_data);
240 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
241 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
242 avctx->stats_out[0] = '\0';
243 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
244 return AVERROR(EINVAL);
247 if (avctx->codec->receive_packet) {
248 ret = avctx->codec->receive_packet(avctx, avpkt);
250 av_packet_unref(avpkt);
252 // Encoders must always return ref-counted buffers.
253 // Side-data only packets have no data and can be not ref-counted.
254 av_assert0(!avpkt->data || avpkt->buf);
256 ret = encode_simple_receive_packet(avctx, avpkt);
258 if (ret == AVERROR_EOF)
259 avci->draining_done = 1;
264 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
266 AVCodecInternal *avci = avctx->internal;
267 AVFrame *dst = avci->buffer_frame;
270 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
271 /* extract audio service type metadata */
272 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
273 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
274 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
276 /* check for valid frame size */
277 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
278 if (src->nb_samples > avctx->frame_size) {
279 av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
280 return AVERROR(EINVAL);
282 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
283 /* if we already got an undersized frame, that must have been the last */
284 if (avctx->internal->last_audio_frame) {
285 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
286 return AVERROR(EINVAL);
289 if (src->nb_samples < avctx->frame_size) {
290 ret = pad_last_frame(avctx, dst, src);
294 avctx->internal->last_audio_frame = 1;
295 } else if (src->nb_samples > avctx->frame_size) {
296 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
297 return AVERROR(EINVAL);
303 ret = av_frame_ref(dst, src);
311 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
313 AVCodecInternal *avci = avctx->internal;
316 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
317 return AVERROR(EINVAL);
322 if (avci->buffer_frame->data[0])
323 return AVERROR(EAGAIN);
328 ret = encode_send_frame_internal(avctx, frame);
333 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
334 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
335 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
342 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
344 AVCodecInternal *avci = avctx->internal;
347 av_packet_unref(avpkt);
349 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
350 return AVERROR(EINVAL);
352 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
353 av_packet_move_ref(avpkt, avci->buffer_pkt);
355 ret = encode_receive_packet_internal(avctx, avpkt);
363 #if FF_API_OLD_ENCDEC
364 static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
365 int *got_packet, const AVFrame *frame)
367 AVCodecInternal *avci = avctx->internal;
373 if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
374 if (frame->format == AV_PIX_FMT_NONE)
375 av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
376 if (frame->width == 0 || frame->height == 0)
377 av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
380 ret = avcodec_send_frame(avctx, frame);
381 if (ret == AVERROR_EOF)
383 else if (ret == AVERROR(EAGAIN)) {
384 /* we fully drain all the output in each encode call, so this should not
390 av_packet_move_ref(&user_pkt, avpkt);
392 ret = avcodec_receive_packet(avctx, avpkt);
394 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
399 if (avpkt != avci->compat_encode_packet) {
400 if (avpkt->data && user_pkt.data) {
401 if (user_pkt.size >= avpkt->size) {
402 memcpy(user_pkt.data, avpkt->data, avpkt->size);
403 av_buffer_unref(&avpkt->buf);
404 avpkt->buf = user_pkt.buf;
405 avpkt->data = user_pkt.data;
406 av_init_packet(&user_pkt);
408 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
409 av_packet_unref(avpkt);
410 ret = AVERROR(EINVAL);
416 avpkt = avci->compat_encode_packet;
418 if (!avci->compat_decode_warned) {
419 av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
420 "API cannot return all the packets for this encoder. "
421 "Some packets will be dropped. Update your code to the "
422 "new encoding API to fix this.\n");
423 avci->compat_decode_warned = 1;
424 av_packet_unref(avpkt);
434 av_packet_unref(&user_pkt);
439 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
441 const AVFrame *frame,
444 int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
447 av_packet_unref(avpkt);
452 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
454 const AVFrame *frame,
457 int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
460 av_packet_unref(avpkt);