• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * generic encoding-related code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/channel_layout.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/samplefmt.h"
28 
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "encode.h"
32 #include "frame_thread_encoder.h"
33 #include "internal.h"
34 
ff_alloc_packet(AVCodecContext * avctx,AVPacket * avpkt,int64_t size)35 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
36 {
37     if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
38         av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
39                size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
40         return AVERROR(EINVAL);
41     }
42 
43     av_assert0(!avpkt->data);
44 
45     av_fast_padded_malloc(&avctx->internal->byte_buffer,
46                           &avctx->internal->byte_buffer_size, size);
47     avpkt->data = avctx->internal->byte_buffer;
48     if (!avpkt->data) {
49         av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
50         return AVERROR(ENOMEM);
51     }
52     avpkt->size = size;
53 
54     return 0;
55 }
56 
avcodec_default_get_encode_buffer(AVCodecContext * avctx,AVPacket * avpkt,int flags)57 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
58 {
59     int ret;
60 
61     if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
62         return AVERROR(EINVAL);
63 
64     if (avpkt->data || avpkt->buf) {
65         av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
66         return AVERROR(EINVAL);
67     }
68 
69     ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
70     if (ret < 0) {
71         av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
72         return ret;
73     }
74     avpkt->data = avpkt->buf->data;
75 
76     return 0;
77 }
78 
ff_get_encode_buffer(AVCodecContext * avctx,AVPacket * avpkt,int64_t size,int flags)79 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
80 {
81     int ret;
82 
83     if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
84         return AVERROR(EINVAL);
85 
86     av_assert0(!avpkt->data && !avpkt->buf);
87 
88     avpkt->size = size;
89     ret = avctx->get_encode_buffer(avctx, avpkt, flags);
90     if (ret < 0)
91         goto fail;
92 
93     if (!avpkt->data || !avpkt->buf) {
94         av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
95         ret = AVERROR(EINVAL);
96         goto fail;
97     }
98     memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
99 
100     ret = 0;
101 fail:
102     if (ret < 0) {
103         av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
104         av_packet_unref(avpkt);
105     }
106 
107     return ret;
108 }
109 
110 /**
111  * Pad last frame with silence.
112  */
pad_last_frame(AVCodecContext * s,AVFrame * frame,const AVFrame * src)113 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
114 {
115     int ret;
116 
117     frame->format         = src->format;
118     frame->nb_samples     = s->frame_size;
119     ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
120     if (ret < 0)
121         goto fail;
122     ret = av_frame_get_buffer(frame, 0);
123     if (ret < 0)
124         goto fail;
125 
126     ret = av_frame_copy_props(frame, src);
127     if (ret < 0)
128         goto fail;
129 
130     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
131                                src->nb_samples, s->ch_layout.nb_channels,
132                                s->sample_fmt)) < 0)
133         goto fail;
134     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
135                                       frame->nb_samples - src->nb_samples,
136                                       s->ch_layout.nb_channels, s->sample_fmt)) < 0)
137         goto fail;
138 
139     return 0;
140 
141 fail:
142     av_frame_unref(frame);
143     return ret;
144 }
145 
avcodec_encode_subtitle(AVCodecContext * avctx,uint8_t * buf,int buf_size,const AVSubtitle * sub)146 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
147                             const AVSubtitle *sub)
148 {
149     int ret;
150     if (sub->start_display_time) {
151         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
152         return -1;
153     }
154 
155     ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
156     avctx->frame_number++;
157     return ret;
158 }
159 
ff_encode_get_frame(AVCodecContext * avctx,AVFrame * frame)160 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
161 {
162     AVCodecInternal *avci = avctx->internal;
163 
164     if (avci->draining)
165         return AVERROR_EOF;
166 
167     if (!avci->buffer_frame->buf[0])
168         return AVERROR(EAGAIN);
169 
170     av_frame_move_ref(frame, avci->buffer_frame);
171 
172     return 0;
173 }
174 
encode_simple_internal(AVCodecContext * avctx,AVPacket * avpkt)175 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
176 {
177     AVCodecInternal   *avci = avctx->internal;
178     AVFrame          *frame = avci->in_frame;
179     const FFCodec *const codec = ffcodec(avctx->codec);
180     int got_packet;
181     int ret;
182 
183     if (avci->draining_done)
184         return AVERROR_EOF;
185 
186     if (!frame->buf[0] && !avci->draining) {
187         av_frame_unref(frame);
188         ret = ff_encode_get_frame(avctx, frame);
189         if (ret < 0 && ret != AVERROR_EOF)
190             return ret;
191     }
192 
193     if (!frame->buf[0]) {
194         if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
195               (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
196             return AVERROR_EOF;
197 
198         // Flushing is signaled with a NULL frame
199         frame = NULL;
200     }
201 
202     got_packet = 0;
203 
204     av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
205 
206     if (CONFIG_FRAME_THREAD_ENCODER &&
207         avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
208         /* This might modify frame, but it doesn't matter, because
209          * the frame properties used below are not used for video
210          * (due to the delay inherent in frame threaded encoding, it makes
211          *  no sense to use the properties of the current frame anyway). */
212         ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
213     else {
214         ret = codec->cb.encode(avctx, avpkt, frame, &got_packet);
215         if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
216             !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
217             avpkt->pts = avpkt->dts = frame->pts;
218     }
219 
220     av_assert0(ret <= 0);
221 
222     emms_c();
223 
224     if (!ret && got_packet) {
225         if (avpkt->data) {
226             ret = av_packet_make_refcounted(avpkt);
227             if (ret < 0)
228                 goto end;
229         }
230 
231         if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
232             if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
233                 if (avpkt->pts == AV_NOPTS_VALUE)
234                     avpkt->pts = frame->pts;
235                 if (!avpkt->duration)
236                     avpkt->duration = ff_samples_to_time_base(avctx,
237                                                               frame->nb_samples);
238             }
239         }
240         if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
241             avpkt->dts = avpkt->pts;
242         }
243         avpkt->flags |= avci->intra_only_flag;
244     }
245 
246     if (avci->draining && !got_packet)
247         avci->draining_done = 1;
248 
249 end:
250     if (ret < 0 || !got_packet)
251         av_packet_unref(avpkt);
252 
253     if (frame)
254         av_frame_unref(frame);
255 
256     if (got_packet)
257         // Encoders must always return ref-counted buffers.
258         // Side-data only packets have no data and can be not ref-counted.
259         av_assert0(!avpkt->data || avpkt->buf);
260 
261     return ret;
262 }
263 
encode_simple_receive_packet(AVCodecContext * avctx,AVPacket * avpkt)264 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
265 {
266     int ret;
267 
268     while (!avpkt->data && !avpkt->side_data) {
269         ret = encode_simple_internal(avctx, avpkt);
270         if (ret < 0)
271             return ret;
272     }
273 
274     return 0;
275 }
276 
encode_receive_packet_internal(AVCodecContext * avctx,AVPacket * avpkt)277 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
278 {
279     AVCodecInternal *avci = avctx->internal;
280     int ret;
281 
282     if (avci->draining_done)
283         return AVERROR_EOF;
284 
285     av_assert0(!avpkt->data && !avpkt->side_data);
286 
287     if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
288         if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
289             avctx->stats_out[0] = '\0';
290         if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
291             return AVERROR(EINVAL);
292     }
293 
294     if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
295         ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
296         if (ret < 0)
297             av_packet_unref(avpkt);
298         else
299             // Encoders must always return ref-counted buffers.
300             // Side-data only packets have no data and can be not ref-counted.
301             av_assert0(!avpkt->data || avpkt->buf);
302     } else
303         ret = encode_simple_receive_packet(avctx, avpkt);
304 
305     if (ret == AVERROR_EOF)
306         avci->draining_done = 1;
307 
308     return ret;
309 }
310 
encode_send_frame_internal(AVCodecContext * avctx,const AVFrame * src)311 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
312 {
313     AVCodecInternal *avci = avctx->internal;
314     AVFrame *dst = avci->buffer_frame;
315     int ret;
316 
317     if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
318         /* extract audio service type metadata */
319         AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
320         if (sd && sd->size >= sizeof(enum AVAudioServiceType))
321             avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
322 
323         /* check for valid frame size */
324         if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
325             if (src->nb_samples > avctx->frame_size) {
326                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
327                 return AVERROR(EINVAL);
328             }
329         } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
330             /* if we already got an undersized frame, that must have been the last */
331             if (avctx->internal->last_audio_frame) {
332                 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
333                 return AVERROR(EINVAL);
334             }
335 
336             if (src->nb_samples < avctx->frame_size) {
337                 ret = pad_last_frame(avctx, dst, src);
338                 if (ret < 0)
339                     return ret;
340 
341                 avctx->internal->last_audio_frame = 1;
342                 return 0;
343             } else if (src->nb_samples > avctx->frame_size) {
344                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
345                 return AVERROR(EINVAL);
346             }
347         }
348     }
349 
350     ret = av_frame_ref(dst, src);
351     if (ret < 0)
352         return ret;
353 
354     return 0;
355 }
356 
avcodec_send_frame(AVCodecContext * avctx,const AVFrame * frame)357 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
358 {
359     AVCodecInternal *avci = avctx->internal;
360     int ret;
361 
362     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
363         return AVERROR(EINVAL);
364 
365     if (avci->draining)
366         return AVERROR_EOF;
367 
368     if (avci->buffer_frame->buf[0])
369         return AVERROR(EAGAIN);
370 
371     if (!frame) {
372         avci->draining = 1;
373     } else {
374         ret = encode_send_frame_internal(avctx, frame);
375         if (ret < 0)
376             return ret;
377     }
378 
379     if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
380         ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
381         if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
382             return ret;
383     }
384 
385     avctx->frame_number++;
386 
387     return 0;
388 }
389 
avcodec_receive_packet(AVCodecContext * avctx,AVPacket * avpkt)390 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
391 {
392     AVCodecInternal *avci = avctx->internal;
393     int ret;
394 
395     av_packet_unref(avpkt);
396 
397     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
398         return AVERROR(EINVAL);
399 
400     if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
401         av_packet_move_ref(avpkt, avci->buffer_pkt);
402     } else {
403         ret = encode_receive_packet_internal(avctx, avpkt);
404         if (ret < 0)
405             return ret;
406     }
407 
408     return 0;
409 }
410 
encode_preinit_video(AVCodecContext * avctx)411 static int encode_preinit_video(AVCodecContext *avctx)
412 {
413     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
414     int i;
415 
416     if (avctx->codec->pix_fmts) {
417         for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
418             if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
419                 break;
420         if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
421             char buf[128];
422             snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
423             av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
424                    (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
425             return AVERROR(EINVAL);
426         }
427         if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
428             avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
429             avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
430             avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
431             avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
432             avctx->color_range = AVCOL_RANGE_JPEG;
433     }
434 
435     if (    avctx->bits_per_raw_sample < 0
436         || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
437         av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
438             avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
439         avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
440     }
441     if (avctx->width <= 0 || avctx->height <= 0) {
442         av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
443         return AVERROR(EINVAL);
444     }
445 
446     if (avctx->ticks_per_frame && avctx->time_base.num &&
447         avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
448         av_log(avctx, AV_LOG_ERROR,
449                "ticks_per_frame %d too large for the timebase %d/%d.",
450                avctx->ticks_per_frame,
451                avctx->time_base.num,
452                avctx->time_base.den);
453         return AVERROR(EINVAL);
454     }
455 
456     if (avctx->hw_frames_ctx) {
457         AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
458         if (frames_ctx->format != avctx->pix_fmt) {
459             av_log(avctx, AV_LOG_ERROR,
460                    "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
461             return AVERROR(EINVAL);
462         }
463         if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
464             avctx->sw_pix_fmt != frames_ctx->sw_format) {
465             av_log(avctx, AV_LOG_ERROR,
466                    "Mismatching AVCodecContext.sw_pix_fmt (%s) "
467                    "and AVHWFramesContext.sw_format (%s)\n",
468                    av_get_pix_fmt_name(avctx->sw_pix_fmt),
469                    av_get_pix_fmt_name(frames_ctx->sw_format));
470             return AVERROR(EINVAL);
471         }
472         avctx->sw_pix_fmt = frames_ctx->sw_format;
473     }
474 
475     return 0;
476 }
477 
encode_preinit_audio(AVCodecContext * avctx)478 static int encode_preinit_audio(AVCodecContext *avctx)
479 {
480     int i;
481 
482     if (avctx->codec->sample_fmts) {
483         for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
484             if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
485                 break;
486             if (avctx->ch_layout.nb_channels == 1 &&
487                 av_get_planar_sample_fmt(avctx->sample_fmt) ==
488                 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
489                 avctx->sample_fmt = avctx->codec->sample_fmts[i];
490                 break;
491             }
492         }
493         if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
494             char buf[128];
495             snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
496             av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
497                    (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
498             return AVERROR(EINVAL);
499         }
500     }
501     if (avctx->codec->supported_samplerates) {
502         for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
503             if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
504                 break;
505         if (avctx->codec->supported_samplerates[i] == 0) {
506             av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
507                    avctx->sample_rate);
508             return AVERROR(EINVAL);
509         }
510     }
511     if (avctx->sample_rate < 0) {
512         av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
513                 avctx->sample_rate);
514         return AVERROR(EINVAL);
515     }
516     if (avctx->codec->ch_layouts) {
517         if (!av_channel_layout_check(&avctx->ch_layout)) {
518             av_log(avctx, AV_LOG_WARNING, "Channel layout not specified correctly\n");
519             return AVERROR(EINVAL);
520         }
521 
522         for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) {
523             if (!av_channel_layout_compare(&avctx->ch_layout, &avctx->codec->ch_layouts[i]))
524                 break;
525         }
526         if (!avctx->codec->ch_layouts[i].nb_channels) {
527             char buf[512];
528             int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
529             if (ret > 0)
530                 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
531             return AVERROR(EINVAL);
532         }
533     }
534 #if FF_API_OLD_CHANNEL_LAYOUT
535 FF_DISABLE_DEPRECATION_WARNINGS
536     if (avctx->channel_layout && avctx->channels) {
537         int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
538         if (channels != avctx->channels) {
539             char buf[512];
540             av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
541             av_log(avctx, AV_LOG_ERROR,
542                    "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
543                    buf, channels, avctx->channels);
544             return AVERROR(EINVAL);
545         }
546     } else if (avctx->channel_layout) {
547         avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
548     }
549     if (avctx->channels < 0) {
550         av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
551                 avctx->channels);
552         return AVERROR(EINVAL);
553     }
554 FF_ENABLE_DEPRECATION_WARNINGS
555 #endif
556 
557     if (!avctx->bits_per_raw_sample)
558         avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
559 
560     return 0;
561 }
562 
ff_encode_preinit(AVCodecContext * avctx)563 int ff_encode_preinit(AVCodecContext *avctx)
564 {
565     AVCodecInternal *avci = avctx->internal;
566     int ret = 0;
567 
568     if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
569         av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
570         return AVERROR(EINVAL);
571     }
572 
573     switch (avctx->codec_type) {
574     case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
575     case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
576     }
577     if (ret < 0)
578         return ret;
579 
580     if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
581         && avctx->bit_rate>0 && avctx->bit_rate<1000) {
582         av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
583     }
584 
585     if (!avctx->rc_initial_buffer_occupancy)
586         avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
587 
588     if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
589         avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
590 
591     if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
592         avci->in_frame = av_frame_alloc();
593         if (!avci->in_frame)
594             return AVERROR(ENOMEM);
595     }
596 
597     return 0;
598 }
599 
ff_encode_alloc_frame(AVCodecContext * avctx,AVFrame * frame)600 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
601 {
602     int ret;
603 
604     switch (avctx->codec->type) {
605     case AVMEDIA_TYPE_VIDEO:
606         frame->format = avctx->pix_fmt;
607         if (frame->width <= 0 || frame->height <= 0) {
608             frame->width  = FFMAX(avctx->width,  avctx->coded_width);
609             frame->height = FFMAX(avctx->height, avctx->coded_height);
610         }
611 
612         break;
613     case AVMEDIA_TYPE_AUDIO:
614         frame->sample_rate = avctx->sample_rate;
615         frame->format      = avctx->sample_fmt;
616         if (!frame->ch_layout.nb_channels) {
617             ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
618             if (ret < 0)
619                 return ret;
620         }
621         break;
622     }
623 
624     ret = avcodec_default_get_buffer2(avctx, frame, 0);
625     if (ret < 0) {
626         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
627         av_frame_unref(frame);
628         return ret;
629     }
630 
631     return 0;
632 }
633