]> git.sesse.net Git - ffmpeg/blob - libavcodec/encode.c
lavc: Add spherical packet side data API
[ffmpeg] / libavcodec / encode.c
1 /*
2  * generic encoding-related code
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
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"
27
28 #include "avcodec.h"
29 #include "internal.h"
30
31 int ff_alloc_packet(AVPacket *avpkt, int size)
32 {
33     if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
34         return AVERROR(EINVAL);
35
36     if (avpkt->data) {
37         AVBufferRef *buf = avpkt->buf;
38
39         if (avpkt->size < size)
40             return AVERROR(EINVAL);
41
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);
48     }
49 }
50
51 /**
52  * Pad last frame with silence.
53  */
54 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
55 {
56     AVFrame *frame = NULL;
57     int ret;
58
59     if (!(frame = av_frame_alloc()))
60         return AVERROR(ENOMEM);
61
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;
68
69     ret = av_frame_copy_props(frame, src);
70     if (ret < 0)
71         goto fail;
72
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;
80
81     *dst = frame;
82
83     return 0;
84
85 fail:
86     av_frame_free(&frame);
87     return ret;
88 }
89
90 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
91                                               AVPacket *avpkt,
92                                               const AVFrame *frame,
93                                               int *got_packet_ptr)
94 {
95     AVFrame tmp;
96     AVFrame *padded_frame = NULL;
97     int ret;
98     int user_packet = !!avpkt->data;
99
100     *got_packet_ptr = 0;
101
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);
105     }
106
107     if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
108         av_packet_unref(avpkt);
109         av_init_packet(avpkt);
110         return 0;
111     }
112
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);
121         }
122         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
123
124         tmp = *frame;
125         tmp.extended_data = tmp.data;
126         frame = &tmp;
127     }
128
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;
134     }
135
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;
147
148                 frame = padded_frame;
149                 avctx->internal->last_audio_frame = 1;
150             }
151
152             if (frame->nb_samples != avctx->frame_size) {
153                 ret = AVERROR(EINVAL);
154                 goto end;
155             }
156         }
157     }
158
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);
168             }
169             avpkt->dts = avpkt->pts;
170         } else {
171             avpkt->size = 0;
172         }
173
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;
178         }
179
180         avctx->frame_number++;
181     }
182
183     if (ret < 0 || !*got_packet_ptr) {
184         av_packet_unref(avpkt);
185         av_init_packet(avpkt);
186         goto end;
187     }
188
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;
193
194 end:
195     av_frame_free(&padded_frame);
196
197 #if FF_API_AUDIOENC_DELAY
198     avctx->delay = avctx->initial_padding;
199 #endif
200
201     return ret;
202 }
203
204 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
205                                               AVPacket *avpkt,
206                                               const AVFrame *frame,
207                                               int *got_packet_ptr)
208 {
209     int ret;
210     int user_packet = !!avpkt->data;
211
212     *got_packet_ptr = 0;
213
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);
217     }
218
219     if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
220         av_packet_unref(avpkt);
221         av_init_packet(avpkt);
222         avpkt->size = 0;
223         return 0;
224     }
225
226     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
227         return AVERROR(EINVAL);
228
229     av_assert0(avctx->codec->encode2);
230
231     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
232     if (!ret) {
233         if (!*got_packet_ptr)
234             avpkt->size = 0;
235         else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
236             avpkt->pts = avpkt->dts = frame->pts;
237
238         if (!user_packet && avpkt->size) {
239             ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
240             if (ret >= 0)
241                 avpkt->data = avpkt->buf->data;
242         }
243
244         avctx->frame_number++;
245     }
246
247     if (ret < 0 || !*got_packet_ptr)
248         av_packet_unref(avpkt);
249
250     emms_c();
251     return ret;
252 }
253
254 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
255                             const AVSubtitle *sub)
256 {
257     int ret;
258     if (sub->start_display_time) {
259         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
260         return -1;
261     }
262     if (sub->num_rects == 0 || !sub->rects)
263         return -1;
264     ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
265     avctx->frame_number++;
266     return ret;
267 }
268
269 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
270 {
271     int ret;
272     *got_packet = 0;
273
274     av_packet_unref(avctx->internal->buffer_pkt);
275     avctx->internal->buffer_pkt_valid = 0;
276
277     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
278         ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
279                                     frame, got_packet);
280     } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
281         ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
282                                     frame, got_packet);
283     } else {
284         ret = AVERROR(EINVAL);
285     }
286
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;
292         ret = 0;
293     } else {
294         av_packet_unref(avctx->internal->buffer_pkt);
295     }
296
297     return ret;
298 }
299
300 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
301 {
302     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
303         return AVERROR(EINVAL);
304
305     if (avctx->internal->draining)
306         return AVERROR_EOF;
307
308     if (!frame) {
309         avctx->internal->draining = 1;
310
311         if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
312             return 0;
313     }
314
315     if (avctx->codec->send_frame)
316         return avctx->codec->send_frame(avctx, frame);
317
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
322     //    needed
323
324     if (avctx->internal->buffer_pkt_valid)
325         return AVERROR(EAGAIN);
326
327     return do_encode(avctx, frame, &(int){0});
328 }
329
330 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
331 {
332     av_packet_unref(avpkt);
333
334     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
335         return AVERROR(EINVAL);
336
337     if (avctx->codec->receive_packet) {
338         if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
339             return AVERROR_EOF;
340         return avctx->codec->receive_packet(avctx, avpkt);
341     }
342
343     // Emulation via old API.
344
345     if (!avctx->internal->buffer_pkt_valid) {
346         int got_packet;
347         int ret;
348         if (!avctx->internal->draining)
349             return AVERROR(EAGAIN);
350         ret = do_encode(avctx, NULL, &got_packet);
351         if (ret < 0)
352             return ret;
353         if (ret >= 0 && !got_packet)
354             return AVERROR_EOF;
355     }
356
357     av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
358     avctx->internal->buffer_pkt_valid = 0;
359     return 0;
360 }