]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/encode.c
avformat: Switch AVChapter.id to 64bits
[ffmpeg] / libavcodec / encode.c
index 8bc10c4abb7633160eddeac13b10c7a36f4db71c..75129c8646f4f9faa2a1d5645d07317ab45b40b0 100644 (file)
@@ -56,6 +56,59 @@ int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64
     return 0;
 }
 
+int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
+{
+    int ret;
+
+    if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(EINVAL);
+
+    if (avpkt->data || avpkt->buf) {
+        av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
+        return AVERROR(EINVAL);
+    }
+
+    ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
+        return ret;
+    }
+    avpkt->data = avpkt->buf->data;
+
+    return 0;
+}
+
+int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
+{
+    int ret;
+
+    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(EINVAL);
+
+    av_assert0(!avpkt->data && !avpkt->buf);
+
+    avpkt->size = size;
+    ret = avctx->get_encode_buffer(avctx, avpkt, flags);
+    if (ret < 0)
+        goto fail;
+
+    if (!avpkt->data || !avpkt->buf) {
+        av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+    memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+
+    ret = 0;
+fail:
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
+        av_packet_unref(avpkt);
+    }
+
+    return ret;
+}
+
 /**
  * Pad last frame with silence.
  */
@@ -152,6 +205,10 @@ static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
 
     if (CONFIG_FRAME_THREAD_ENCODER &&
         avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
+        /* This might modify frame, but it doesn't matter, because
+         * the frame properties used below are not used for video
+         * (due to the delay inherent in frame threaded encoding, it makes
+         *  no sense to use the properties of the current frame anyway). */
         ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
     else {
         ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
@@ -242,7 +299,9 @@ static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt
 
     if (avctx->codec->receive_packet) {
         ret = avctx->codec->receive_packet(avctx, avpkt);
-        if (!ret)
+        if (ret < 0)
+            av_packet_unref(avpkt);
+        else
             // Encoders must always return ref-counted buffers.
             // Side-data only packets have no data and can be not ref-counted.
             av_assert0(!avpkt->data || avpkt->buf);
@@ -354,6 +413,7 @@ int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *
     return 0;
 }
 
+#if FF_API_OLD_ENCDEC
 static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
                          int *got_packet, const AVFrame *frame)
 {
@@ -370,6 +430,12 @@ static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
             av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
     }
 
+    if (avctx->codec->capabilities & AV_CODEC_CAP_DR1) {
+        av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* API does not support "
+                                      "AV_CODEC_CAP_DR1 encoders\n");
+        return AVERROR(ENOSYS);
+    }
+
     ret = avcodec_send_frame(avctx, frame);
     if (ret == AVERROR_EOF)
         ret = 0;
@@ -396,7 +462,9 @@ static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
                     av_buffer_unref(&avpkt->buf);
                     avpkt->buf  = user_pkt.buf;
                     avpkt->data = user_pkt.data;
+FF_DISABLE_DEPRECATION_WARNINGS
                     av_init_packet(&user_pkt);
+FF_ENABLE_DEPRECATION_WARNINGS
                 } else {
                     av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
                     av_packet_unref(avpkt);
@@ -454,3 +522,159 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
 
     return ret;
 }
+#endif
+
+int ff_encode_preinit(AVCodecContext *avctx)
+{
+    int i;
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+    avctx->coded_frame = av_frame_alloc();
+    if (!avctx->coded_frame) {
+        return AVERROR(ENOMEM);
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+    if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
+        av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (avctx->codec->sample_fmts) {
+        for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
+            if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
+                break;
+            if (avctx->channels == 1 &&
+                av_get_planar_sample_fmt(avctx->sample_fmt) ==
+                av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
+                avctx->sample_fmt = avctx->codec->sample_fmts[i];
+                break;
+            }
+        }
+        if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
+            char buf[128];
+            snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
+            av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
+                   (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
+            return AVERROR(EINVAL);
+        }
+    }
+    if (avctx->codec->pix_fmts) {
+        for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
+            if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
+                break;
+        if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
+            char buf[128];
+            snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
+            av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
+                   (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
+            return AVERROR(EINVAL);
+        }
+        if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
+            avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
+            avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
+            avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
+            avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
+            avctx->color_range = AVCOL_RANGE_JPEG;
+    }
+    if (avctx->codec->supported_samplerates) {
+        for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
+            if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
+                break;
+        if (avctx->codec->supported_samplerates[i] == 0) {
+            av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
+                   avctx->sample_rate);
+            return AVERROR(EINVAL);
+        }
+    }
+    if (avctx->sample_rate < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
+                avctx->sample_rate);
+        return AVERROR(EINVAL);
+    }
+    if (avctx->codec->channel_layouts) {
+        if (!avctx->channel_layout) {
+            av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
+        } else {
+            for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
+                if (avctx->channel_layout == avctx->codec->channel_layouts[i])
+                    break;
+            if (avctx->codec->channel_layouts[i] == 0) {
+                char buf[512];
+                av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
+                av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
+                return AVERROR(EINVAL);
+            }
+        }
+    }
+    if (avctx->channel_layout && avctx->channels) {
+        int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
+        if (channels != avctx->channels) {
+            char buf[512];
+            av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
+            av_log(avctx, AV_LOG_ERROR,
+                   "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
+                   buf, channels, avctx->channels);
+            return AVERROR(EINVAL);
+        }
+    } else if (avctx->channel_layout) {
+        avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
+    }
+    if (avctx->channels < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
+                avctx->channels);
+        return AVERROR(EINVAL);
+    }
+    if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
+        if (    avctx->bits_per_raw_sample < 0
+            || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
+            av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
+                avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
+            avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
+        }
+        if (avctx->width <= 0 || avctx->height <= 0) {
+            av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
+            return AVERROR(EINVAL);
+        }
+    }
+    if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
+        && avctx->bit_rate>0 && avctx->bit_rate<1000) {
+        av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
+    }
+
+    if (!avctx->rc_initial_buffer_occupancy)
+        avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
+
+    if (avctx->ticks_per_frame && avctx->time_base.num &&
+        avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
+        av_log(avctx, AV_LOG_ERROR,
+               "ticks_per_frame %d too large for the timebase %d/%d.",
+               avctx->ticks_per_frame,
+               avctx->time_base.num,
+               avctx->time_base.den);
+        return AVERROR(EINVAL);
+    }
+
+    if (avctx->hw_frames_ctx) {
+        AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+        if (frames_ctx->format != avctx->pix_fmt) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
+            return AVERROR(EINVAL);
+        }
+        if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
+            avctx->sw_pix_fmt != frames_ctx->sw_format) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Mismatching AVCodecContext.sw_pix_fmt (%s) "
+                   "and AVHWFramesContext.sw_format (%s)\n",
+                   av_get_pix_fmt_name(avctx->sw_pix_fmt),
+                   av_get_pix_fmt_name(frames_ctx->sw_format));
+            return AVERROR(EINVAL);
+        }
+        avctx->sw_pix_fmt = frames_ctx->sw_format;
+    }
+
+    return 0;
+}