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
);
200 int attribute_align_arg
avcodec_encode_video2(AVCodecContext
*avctx
,
202 const AVFrame
*frame
,
206 int user_packet
= !!avpkt
->data
;
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
);
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
);
229 if (!*got_packet_ptr
)
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
);
237 avpkt
->data
= avpkt
->buf
->data
;
240 avctx
->frame_number
++;
243 if (ret
< 0 || !*got_packet_ptr
)
244 av_packet_unref(avpkt
);
250 int avcodec_encode_subtitle(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
251 const AVSubtitle
*sub
)
254 if (sub
->start_display_time
) {
255 av_log(avctx
, AV_LOG_ERROR
, "start_display_time must be 0.\n");
258 if (sub
->num_rects
== 0 || !sub
->rects
)
260 ret
= avctx
->codec
->encode_sub(avctx
, buf
, buf_size
, sub
);
261 avctx
->frame_number
++;
265 static int do_encode(AVCodecContext
*avctx
, const AVFrame
*frame
, int *got_packet
)
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
,
276 } else if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
277 ret
= avcodec_encode_audio2(avctx
, avctx
->internal
->buffer_pkt
,
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;
290 av_packet_unref(avctx
->internal
->buffer_pkt
);
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
)
305 avctx
->internal
->draining
= 1;
307 if (!(avctx
->codec
->capabilities
& AV_CODEC_CAP_DELAY
))
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
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
))
336 return avctx
->codec
->receive_packet(avctx
, avpkt
);
339 // Emulation via old API.
341 if (!avctx
->internal
->buffer_pkt_valid
) {
344 if (!avctx
->internal
->draining
)
345 return AVERROR(EAGAIN
);
346 ret
= do_encode(avctx
, NULL
, &got_packet
);
349 if (ret
>= 0 && !got_packet
)
353 av_packet_move_ref(avpkt
, avctx
->internal
->buffer_pkt
);
354 avctx
->internal
->buffer_pkt_valid
= 0;