]> git.sesse.net Git - ffmpeg/blob - libavcodec/encode.c
lavf/qsvvpp: bypass vpp if not needed.
[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     return ret;
198 }
199
200 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
201                                               AVPacket *avpkt,
202                                               const AVFrame *frame,
203                                               int *got_packet_ptr)
204 {
205     int ret;
206     int user_packet = !!avpkt->data;
207
208     *got_packet_ptr = 0;
209
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);
213     }
214
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;
220     }
221
222     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
223         return AVERROR(EINVAL);
224
225     av_assert0(avctx->codec->encode2);
226
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;
233
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;
238         }
239
240         avctx->frame_number++;
241     }
242
243     if (ret < 0 || !*got_packet_ptr)
244         av_packet_unref(avpkt);
245
246     emms_c();
247     return ret;
248 }
249
250 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
251                             const AVSubtitle *sub)
252 {
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;
257     }
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;
263 }
264
265 static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
266 {
267     int ret;
268     *got_packet = 0;
269
270     av_packet_unref(avctx->internal->buffer_pkt);
271     avctx->internal->buffer_pkt_valid = 0;
272
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);
281     }
282
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);
291     }
292
293     return ret;
294 }
295
296 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
297 {
298     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
299         return AVERROR(EINVAL);
300
301     if (avctx->internal->draining)
302         return AVERROR_EOF;
303
304     if (!frame) {
305         avctx->internal->draining = 1;
306
307         if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
308             return 0;
309     }
310
311     if (avctx->codec->send_frame)
312         return avctx->codec->send_frame(avctx, frame);
313
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
319
320     if (avctx->internal->buffer_pkt_valid)
321         return AVERROR(EAGAIN);
322
323     return do_encode(avctx, frame, &(int){0});
324 }
325
326 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
327 {
328     av_packet_unref(avpkt);
329
330     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
331         return AVERROR(EINVAL);
332
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);
337     }
338
339     // Emulation via old API.
340
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;
351     }
352
353     av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
354     avctx->internal->buffer_pkt_valid = 0;
355     return 0;
356 }