2 * generic encoding-related code
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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"
31 int ff_alloc_packet(AVPacket *avpkt, int size)
33 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
34 return AVERROR(EINVAL);
37 AVBufferRef *buf = avpkt->buf;
39 if (avpkt->size < size)
40 return AVERROR(EINVAL);
42 av_init_packet(avpkt);
47 return av_new_packet(avpkt, size);
52 * Pad last frame with silence.
54 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
56 AVFrame *frame = NULL;
59 if (!(frame = av_frame_alloc()))
60 return AVERROR(ENOMEM);
62 frame->format = src->format;
63 frame->channel_layout = src->channel_layout;
64 frame->nb_samples = s->frame_size;
65 ret = av_frame_get_buffer(frame, 32);
69 ret = av_frame_copy_props(frame, src);
73 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
74 src->nb_samples, s->channels, s->sample_fmt)) < 0)
76 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
77 frame->nb_samples - src->nb_samples,
78 s->channels, s->sample_fmt)) < 0)
86 av_frame_free(&frame);
90 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
96 AVFrame *padded_frame = NULL;
98 int user_packet = !!avpkt->data;
102 if (!avctx->codec->encode2) {
103 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
104 return AVERROR(ENOSYS);
107 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
108 av_packet_unref(avpkt);
109 av_init_packet(avpkt);
113 /* ensure that extended_data is properly set */
114 if (frame && !frame->extended_data) {
115 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
116 avctx->channels > AV_NUM_DATA_POINTERS) {
117 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
118 "with more than %d channels, but extended_data is not set.\n",
119 AV_NUM_DATA_POINTERS);
120 return AVERROR(EINVAL);
122 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
125 tmp.extended_data = tmp.data;
129 /* extract audio service type metadata */
131 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
132 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
133 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
136 /* check for valid frame size */
138 if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
139 if (frame->nb_samples > avctx->frame_size)
140 return AVERROR(EINVAL);
141 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
142 if (frame->nb_samples < avctx->frame_size &&
143 !avctx->internal->last_audio_frame) {
144 ret = pad_last_frame(avctx, &padded_frame, frame);
148 frame = padded_frame;
149 avctx->internal->last_audio_frame = 1;
152 if (frame->nb_samples != avctx->frame_size) {
153 ret = AVERROR(EINVAL);
159 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
161 if (*got_packet_ptr) {
162 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
163 if (avpkt->pts == AV_NOPTS_VALUE)
164 avpkt->pts = frame->pts;
165 if (!avpkt->duration)
166 avpkt->duration = ff_samples_to_time_base(avctx,
169 avpkt->dts = avpkt->pts;
174 if (!user_packet && avpkt->size) {
175 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
177 avpkt->data = avpkt->buf->data;
180 avctx->frame_number++;
183 if (ret < 0 || !*got_packet_ptr) {
184 av_packet_unref(avpkt);
185 av_init_packet(avpkt);
189 /* NOTE: if we add any audio encoders which output non-keyframe packets,
190 * this needs to be moved to the encoders, but for now we can do it
191 * here to simplify things */
192 avpkt->flags |= AV_PKT_FLAG_KEY;
195 av_frame_free(&padded_frame);
197 #if FF_API_AUDIOENC_DELAY
198 avctx->delay = avctx->initial_padding;
204 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
206 const AVFrame *frame,
210 int user_packet = !!avpkt->data;
214 if (!avctx->codec->encode2) {
215 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
216 return AVERROR(ENOSYS);
219 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
220 av_packet_unref(avpkt);
221 av_init_packet(avpkt);
226 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
227 return AVERROR(EINVAL);
229 av_assert0(avctx->codec->encode2);
231 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
233 if (!*got_packet_ptr)
235 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
236 avpkt->pts = avpkt->dts = frame->pts;
238 if (!user_packet && avpkt->size) {
239 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
241 avpkt->data = avpkt->buf->data;
244 avctx->frame_number++;
247 if (ret < 0 || !*got_packet_ptr)
248 av_packet_unref(avpkt);
254 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
255 const AVSubtitle *sub)
258 if (sub->start_display_time) {
259 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
262 if (sub->num_rects == 0 || !sub->rects)
264 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
265 avctx->frame_number++;
269 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
274 av_packet_unref(avctx->internal->buffer_pkt);
275 avctx->internal->buffer_pkt_valid = 0;
277 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
278 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
280 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
281 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
284 ret = AVERROR(EINVAL);
287 if (ret >= 0 && *got_packet) {
288 // Encoders must always return ref-counted buffers.
289 // Side-data only packets have no data and can be not ref-counted.
290 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
291 avctx->internal->buffer_pkt_valid = 1;
294 av_packet_unref(avctx->internal->buffer_pkt);
300 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
302 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
303 return AVERROR(EINVAL);
305 if (avctx->internal->draining)
309 avctx->internal->draining = 1;
311 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
315 if (avctx->codec->send_frame)
316 return avctx->codec->send_frame(avctx, frame);
318 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
319 // 1. if the AVFrame is not refcounted, the copying will be much more
320 // expensive than copying the packet data
321 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
324 if (avctx->internal->buffer_pkt_valid)
325 return AVERROR(EAGAIN);
327 return do_encode(avctx, frame, &(int){0});
330 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
332 av_packet_unref(avpkt);
334 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
335 return AVERROR(EINVAL);
337 if (avctx->codec->receive_packet) {
338 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
340 return avctx->codec->receive_packet(avctx, avpkt);
343 // Emulation via old API.
345 if (!avctx->internal->buffer_pkt_valid) {
348 if (!avctx->internal->draining)
349 return AVERROR(EAGAIN);
350 ret = do_encode(avctx, NULL, &got_packet);
353 if (ret >= 0 && !got_packet)
357 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
358 avctx->internal->buffer_pkt_valid = 0;