aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / encode.c
blob9bb7ae5bde0688ea55498903d28dfaf85e9f51d2
1 /*
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"
28 #include "avcodec.h"
29 #include "internal.h"
31 int ff_alloc_packet(AVPacket *avpkt, int size)
33 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
34 return AVERROR(EINVAL);
36 if (avpkt->data) {
37 AVBufferRef *buf = avpkt->buf;
39 if (avpkt->size < size)
40 return AVERROR(EINVAL);
42 av_init_packet(avpkt);
43 avpkt->buf = buf;
44 avpkt->size = size;
45 return 0;
46 } else {
47 return av_new_packet(avpkt, size);
51 /**
52 * Pad last frame with silence.
54 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
56 AVFrame *frame = NULL;
57 int ret;
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);
66 if (ret < 0)
67 goto fail;
69 ret = av_frame_copy_props(frame, src);
70 if (ret < 0)
71 goto fail;
73 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
74 src->nb_samples, s->channels, s->sample_fmt)) < 0)
75 goto fail;
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)
79 goto fail;
81 *dst = frame;
83 return 0;
85 fail:
86 av_frame_free(&frame);
87 return ret;
90 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
91 AVPacket *avpkt,
92 const AVFrame *frame,
93 int *got_packet_ptr)
95 AVFrame tmp;
96 AVFrame *padded_frame = NULL;
97 int ret;
98 int user_packet = !!avpkt->data;
100 *got_packet_ptr = 0;
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);
110 return 0;
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");
124 tmp = *frame;
125 tmp.extended_data = tmp.data;
126 frame = &tmp;
129 /* extract audio service type metadata */
130 if (frame) {
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 */
137 if (frame) {
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);
145 if (ret < 0)
146 return ret;
148 frame = padded_frame;
149 avctx->internal->last_audio_frame = 1;
152 if (frame->nb_samples != avctx->frame_size) {
153 ret = AVERROR(EINVAL);
154 goto end;
159 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
160 if (!ret) {
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,
167 frame->nb_samples);
169 avpkt->dts = avpkt->pts;
170 } else {
171 avpkt->size = 0;
174 if (!user_packet && avpkt->size) {
175 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
176 if (ret >= 0)
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);
186 goto end;
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;
194 end:
195 av_frame_free(&padded_frame);
197 return ret;
200 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
201 AVPacket *avpkt,
202 const AVFrame *frame,
203 int *got_packet_ptr)
205 int ret;
206 int user_packet = !!avpkt->data;
208 *got_packet_ptr = 0;
210 if (!avctx->codec->encode2) {
211 av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
212 return AVERROR(ENOSYS);
215 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
216 av_packet_unref(avpkt);
217 av_init_packet(avpkt);
218 avpkt->size = 0;
219 return 0;
222 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
223 return AVERROR(EINVAL);
225 av_assert0(avctx->codec->encode2);
227 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
228 if (!ret) {
229 if (!*got_packet_ptr)
230 avpkt->size = 0;
231 else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
232 avpkt->pts = avpkt->dts = frame->pts;
234 if (!user_packet && avpkt->size) {
235 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
236 if (ret >= 0)
237 avpkt->data = avpkt->buf->data;
240 avctx->frame_number++;
243 if (ret < 0 || !*got_packet_ptr)
244 av_packet_unref(avpkt);
246 emms_c();
247 return ret;
250 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
251 const AVSubtitle *sub)
253 int ret;
254 if (sub->start_display_time) {
255 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
256 return -1;
258 if (sub->num_rects == 0 || !sub->rects)
259 return -1;
260 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
261 avctx->frame_number++;
262 return ret;
265 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
267 int ret;
268 *got_packet = 0;
270 av_packet_unref(avctx->internal->buffer_pkt);
271 avctx->internal->buffer_pkt_valid = 0;
273 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
274 ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
275 frame, got_packet);
276 } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
277 ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
278 frame, got_packet);
279 } else {
280 ret = AVERROR(EINVAL);
283 if (ret >= 0 && *got_packet) {
284 // Encoders must always return ref-counted buffers.
285 // Side-data only packets have no data and can be not ref-counted.
286 av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
287 avctx->internal->buffer_pkt_valid = 1;
288 ret = 0;
289 } else {
290 av_packet_unref(avctx->internal->buffer_pkt);
293 return ret;
296 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
298 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
299 return AVERROR(EINVAL);
301 if (avctx->internal->draining)
302 return AVERROR_EOF;
304 if (!frame) {
305 avctx->internal->draining = 1;
307 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
308 return 0;
311 if (avctx->codec->send_frame)
312 return avctx->codec->send_frame(avctx, frame);
314 // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
315 // 1. if the AVFrame is not refcounted, the copying will be much more
316 // expensive than copying the packet data
317 // 2. assume few users use non-refcounted AVPackets, so usually no copy is
318 // needed
320 if (avctx->internal->buffer_pkt_valid)
321 return AVERROR(EAGAIN);
323 return do_encode(avctx, frame, &(int){0});
326 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
328 av_packet_unref(avpkt);
330 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
331 return AVERROR(EINVAL);
333 if (avctx->codec->receive_packet) {
334 if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
335 return AVERROR_EOF;
336 return avctx->codec->receive_packet(avctx, avpkt);
339 // Emulation via old API.
341 if (!avctx->internal->buffer_pkt_valid) {
342 int got_packet;
343 int ret;
344 if (!avctx->internal->draining)
345 return AVERROR(EAGAIN);
346 ret = do_encode(avctx, NULL, &got_packet);
347 if (ret < 0)
348 return ret;
349 if (ret >= 0 && !got_packet)
350 return AVERROR_EOF;
353 av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
354 avctx->internal->buffer_pkt_valid = 0;
355 return 0;