LCOV - code coverage report
Current view: top level - libavcodec - encode.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 177 249 71.1 %
Date: 2018-05-20 11:54:08 Functions: 8 9 88.9 %

          Line data    Source code
       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/frame.h"
      24             : #include "libavutil/imgutils.h"
      25             : #include "libavutil/internal.h"
      26             : #include "libavutil/samplefmt.h"
      27             : 
      28             : #include "avcodec.h"
      29             : #include "frame_thread_encoder.h"
      30             : #include "internal.h"
      31             : 
      32      391236 : int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
      33             : {
      34      391236 :     if (avpkt->size < 0) {
      35           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
      36           0 :         return AVERROR(EINVAL);
      37             :     }
      38      391236 :     if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
      39           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
      40             :                size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
      41           0 :         return AVERROR(EINVAL);
      42             :     }
      43             : 
      44      391236 :     if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
      45       65110 :         av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
      46       65110 :         if (!avpkt->data || avpkt->size < size) {
      47       65110 :             av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
      48       65110 :             avpkt->data = avctx->internal->byte_buffer;
      49       65110 :             avpkt->size = avctx->internal->byte_buffer_size;
      50             :         }
      51             :     }
      52             : 
      53      391236 :     if (avpkt->data) {
      54       65110 :         AVBufferRef *buf = avpkt->buf;
      55             : 
      56       65110 :         if (avpkt->size < size) {
      57           0 :             av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
      58           0 :             return AVERROR(EINVAL);
      59             :         }
      60             : 
      61       65110 :         av_init_packet(avpkt);
      62       65110 :         avpkt->buf      = buf;
      63       65110 :         avpkt->size     = size;
      64       65110 :         return 0;
      65             :     } else {
      66      326126 :         int ret = av_new_packet(avpkt, size);
      67      326126 :         if (ret < 0)
      68           0 :             av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
      69      326126 :         return ret;
      70             :     }
      71             : }
      72             : 
      73           0 : int ff_alloc_packet(AVPacket *avpkt, int size)
      74             : {
      75           0 :     return ff_alloc_packet2(NULL, avpkt, size, 0);
      76             : }
      77             : 
      78             : /**
      79             :  * Pad last frame with silence.
      80             :  */
      81          39 : static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
      82             : {
      83          39 :     AVFrame *frame = NULL;
      84             :     int ret;
      85             : 
      86          39 :     if (!(frame = av_frame_alloc()))
      87           0 :         return AVERROR(ENOMEM);
      88             : 
      89          39 :     frame->format         = src->format;
      90          39 :     frame->channel_layout = src->channel_layout;
      91          39 :     frame->channels       = src->channels;
      92          39 :     frame->nb_samples     = s->frame_size;
      93          39 :     ret = av_frame_get_buffer(frame, 32);
      94          39 :     if (ret < 0)
      95           0 :         goto fail;
      96             : 
      97          39 :     ret = av_frame_copy_props(frame, src);
      98          39 :     if (ret < 0)
      99           0 :         goto fail;
     100             : 
     101          39 :     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
     102             :                                src->nb_samples, s->channels, s->sample_fmt)) < 0)
     103           0 :         goto fail;
     104          78 :     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
     105          39 :                                       frame->nb_samples - src->nb_samples,
     106             :                                       s->channels, s->sample_fmt)) < 0)
     107           0 :         goto fail;
     108             : 
     109          39 :     *dst = frame;
     110             : 
     111          39 :     return 0;
     112             : 
     113           0 : fail:
     114           0 :     av_frame_free(&frame);
     115           0 :     return ret;
     116             : }
     117             : 
     118      300684 : int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
     119             :                                               AVPacket *avpkt,
     120             :                                               const AVFrame *frame,
     121             :                                               int *got_packet_ptr)
     122             : {
     123      300684 :     AVFrame *extended_frame = NULL;
     124      300684 :     AVFrame *padded_frame = NULL;
     125             :     int ret;
     126      300684 :     AVPacket user_pkt = *avpkt;
     127      300684 :     int needs_realloc = !user_pkt.data;
     128             : 
     129      300684 :     *got_packet_ptr = 0;
     130             : 
     131      300684 :     if (!avctx->codec->encode2) {
     132           0 :         av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
     133           0 :         return AVERROR(ENOSYS);
     134             :     }
     135             : 
     136      300684 :     if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
     137          58 :         av_packet_unref(avpkt);
     138          58 :         av_init_packet(avpkt);
     139          58 :         return 0;
     140             :     }
     141             : 
     142             :     /* ensure that extended_data is properly set */
     143      300626 :     if (frame && !frame->extended_data) {
     144           0 :         if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
     145           0 :             avctx->channels > AV_NUM_DATA_POINTERS) {
     146           0 :             av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
     147             :                                         "with more than %d channels, but extended_data is not set.\n",
     148             :                    AV_NUM_DATA_POINTERS);
     149           0 :             return AVERROR(EINVAL);
     150             :         }
     151           0 :         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
     152             : 
     153           0 :         extended_frame = av_frame_alloc();
     154           0 :         if (!extended_frame)
     155           0 :             return AVERROR(ENOMEM);
     156             : 
     157           0 :         memcpy(extended_frame, frame, sizeof(AVFrame));
     158           0 :         extended_frame->extended_data = extended_frame->data;
     159           0 :         frame = extended_frame;
     160             :     }
     161             : 
     162             :     /* extract audio service type metadata */
     163      300626 :     if (frame) {
     164      300561 :         AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
     165      300561 :         if (sd && sd->size >= sizeof(enum AVAudioServiceType))
     166           0 :             avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
     167             :     }
     168             : 
     169             :     /* check for valid frame size */
     170      300626 :     if (frame) {
     171      300561 :         if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
     172       11673 :             if (frame->nb_samples > avctx->frame_size) {
     173           0 :                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
     174           0 :                 ret = AVERROR(EINVAL);
     175           0 :                 goto end;
     176             :             }
     177      288888 :         } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
     178       39309 :             if (frame->nb_samples < avctx->frame_size &&
     179          39 :                 !avctx->internal->last_audio_frame) {
     180          39 :                 ret = pad_last_frame(avctx, &padded_frame, frame);
     181          39 :                 if (ret < 0)
     182           0 :                     goto end;
     183             : 
     184          39 :                 frame = padded_frame;
     185          39 :                 avctx->internal->last_audio_frame = 1;
     186             :             }
     187             : 
     188       39270 :             if (frame->nb_samples != avctx->frame_size) {
     189           0 :                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
     190           0 :                 ret = AVERROR(EINVAL);
     191           0 :                 goto end;
     192             :             }
     193             :         }
     194             :     }
     195             : 
     196      300626 :     av_assert0(avctx->codec->encode2);
     197             : 
     198      300626 :     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
     199      300626 :     if (!ret) {
     200      300626 :         if (*got_packet_ptr) {
     201      294442 :             if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
     202      284919 :                 if (avpkt->pts == AV_NOPTS_VALUE)
     203      278909 :                     avpkt->pts = frame->pts;
     204      284919 :                 if (!avpkt->duration)
     205      283864 :                     avpkt->duration = ff_samples_to_time_base(avctx,
     206      283864 :                                                               frame->nb_samples);
     207             :             }
     208      294442 :             avpkt->dts = avpkt->pts;
     209             :         } else {
     210        6184 :             avpkt->size = 0;
     211             :         }
     212             :     }
     213      300626 :     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
     214       45072 :         needs_realloc = 0;
     215       45072 :         if (user_pkt.data) {
     216           0 :             if (user_pkt.size >= avpkt->size) {
     217           0 :                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
     218             :             } else {
     219           0 :                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
     220           0 :                 avpkt->size = user_pkt.size;
     221           0 :                 ret = -1;
     222             :             }
     223           0 :             avpkt->buf      = user_pkt.buf;
     224           0 :             avpkt->data     = user_pkt.data;
     225       45072 :         } else if (!avpkt->buf) {
     226       45072 :             ret = av_packet_make_refcounted(avpkt);
     227       45072 :             if (ret < 0)
     228           0 :                 goto end;
     229             :         }
     230             :     }
     231             : 
     232      300626 :     if (!ret) {
     233      300626 :         if (needs_realloc && avpkt->data) {
     234      249358 :             ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
     235      249358 :             if (ret >= 0)
     236      249358 :                 avpkt->data = avpkt->buf->data;
     237             :         }
     238             : 
     239      300626 :         avctx->frame_number++;
     240             :     }
     241             : 
     242      300626 :     if (ret < 0 || !*got_packet_ptr) {
     243        6184 :         av_packet_unref(avpkt);
     244        6184 :         av_init_packet(avpkt);
     245        6184 :         goto end;
     246             :     }
     247             : 
     248             :     /* NOTE: if we add any audio encoders which output non-keyframe packets,
     249             :      *       this needs to be moved to the encoders, but for now we can do it
     250             :      *       here to simplify things */
     251      294442 :     avpkt->flags |= AV_PKT_FLAG_KEY;
     252             : 
     253      300626 : end:
     254      300626 :     av_frame_free(&padded_frame);
     255      300626 :     av_free(extended_frame);
     256             : 
     257      300626 :     return ret;
     258             : }
     259             : 
     260      102284 : int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
     261             :                                               AVPacket *avpkt,
     262             :                                               const AVFrame *frame,
     263             :                                               int *got_packet_ptr)
     264             : {
     265             :     int ret;
     266      102284 :     AVPacket user_pkt = *avpkt;
     267      102284 :     int needs_realloc = !user_pkt.data;
     268             : 
     269      102284 :     *got_packet_ptr = 0;
     270             : 
     271      102284 :     if (!avctx->codec->encode2) {
     272           0 :         av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
     273           0 :         return AVERROR(ENOSYS);
     274             :     }
     275             : 
     276      102284 :     if(CONFIG_FRAME_THREAD_ENCODER &&
     277      103909 :        avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
     278         860 :         return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
     279             : 
     280      101424 :     if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
     281           0 :         avctx->stats_out[0] = '\0';
     282             : 
     283      101424 :     if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
     284        4254 :         av_packet_unref(avpkt);
     285        4254 :         av_init_packet(avpkt);
     286        4254 :         avpkt->size = 0;
     287        4254 :         return 0;
     288             :     }
     289             : 
     290       97170 :     if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
     291           0 :         return AVERROR(EINVAL);
     292             : 
     293       97170 :     if (frame && frame->format == AV_PIX_FMT_NONE)
     294           0 :         av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
     295       97170 :     if (frame && (frame->width == 0 || frame->height == 0))
     296           0 :         av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
     297             : 
     298       97170 :     av_assert0(avctx->codec->encode2);
     299             : 
     300       97170 :     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
     301       97170 :     av_assert0(ret <= 0);
     302             : 
     303       97170 :     emms_c();
     304             : 
     305       97170 :     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
     306       20038 :         needs_realloc = 0;
     307       20038 :         if (user_pkt.data) {
     308           0 :             if (user_pkt.size >= avpkt->size) {
     309           0 :                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
     310             :             } else {
     311           0 :                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
     312           0 :                 avpkt->size = user_pkt.size;
     313           0 :                 ret = -1;
     314             :             }
     315           0 :             avpkt->buf      = user_pkt.buf;
     316           0 :             avpkt->data     = user_pkt.data;
     317       20038 :         } else if (!avpkt->buf) {
     318       20038 :             ret = av_packet_make_refcounted(avpkt);
     319       20038 :             if (ret < 0)
     320           0 :                 return ret;
     321             :         }
     322             :     }
     323             : 
     324       97170 :     if (!ret) {
     325       97170 :         if (!*got_packet_ptr)
     326         319 :             avpkt->size = 0;
     327       96851 :         else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
     328       90820 :             avpkt->pts = avpkt->dts = frame->pts;
     329             : 
     330       97170 :         if (needs_realloc && avpkt->data) {
     331       76813 :             ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
     332       76813 :             if (ret >= 0)
     333       76813 :                 avpkt->data = avpkt->buf->data;
     334             :         }
     335             : 
     336       97170 :         avctx->frame_number++;
     337             :     }
     338             : 
     339       97170 :     if (ret < 0 || !*got_packet_ptr)
     340         319 :         av_packet_unref(avpkt);
     341             : 
     342       97170 :     return ret;
     343             : }
     344             : 
     345         595 : int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
     346             :                             const AVSubtitle *sub)
     347             : {
     348             :     int ret;
     349         595 :     if (sub->start_display_time) {
     350           0 :         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
     351           0 :         return -1;
     352             :     }
     353             : 
     354         595 :     ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
     355         595 :     avctx->frame_number++;
     356         595 :     return ret;
     357             : }
     358             : 
     359      398943 : static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
     360             : {
     361             :     int ret;
     362      398943 :     *got_packet = 0;
     363             : 
     364      398943 :     av_packet_unref(avctx->internal->buffer_pkt);
     365      398943 :     avctx->internal->buffer_pkt_valid = 0;
     366             : 
     367      398943 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
     368      101459 :         ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
     369             :                                     frame, got_packet);
     370      297484 :     } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
     371      297484 :         ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
     372             :                                     frame, got_packet);
     373             :     } else {
     374           0 :         ret = AVERROR(EINVAL);
     375             :     }
     376             : 
     377      398943 :     if (ret >= 0 && *got_packet) {
     378             :         // Encoders must always return ref-counted buffers.
     379             :         // Side-data only packets have no data and can be not ref-counted.
     380      388033 :         av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
     381      388033 :         avctx->internal->buffer_pkt_valid = 1;
     382      388033 :         ret = 0;
     383             :     } else {
     384       10910 :         av_packet_unref(avctx->internal->buffer_pkt);
     385             :     }
     386             : 
     387      398943 :     return ret;
     388             : }
     389             : 
     390      398645 : int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
     391             : {
     392      398645 :     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
     393           0 :         return AVERROR(EINVAL);
     394             : 
     395      398645 :     if (avctx->internal->draining)
     396           0 :         return AVERROR_EOF;
     397             : 
     398      398645 :     if (!frame) {
     399        4493 :         avctx->internal->draining = 1;
     400             : 
     401        4493 :         if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
     402        4330 :             return 0;
     403             :     }
     404             : 
     405      394315 :     if (avctx->codec->send_frame)
     406           0 :         return avctx->codec->send_frame(avctx, frame);
     407             : 
     408             :     // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
     409             :     // 1. if the AVFrame is not refcounted, the copying will be much more
     410             :     //    expensive than copying the packet data
     411             :     // 2. assume few users use non-refcounted AVPackets, so usually no copy is
     412             :     //    needed
     413             : 
     414      394315 :     if (avctx->internal->buffer_pkt_valid)
     415           0 :         return AVERROR(EAGAIN);
     416             : 
     417      394315 :     return do_encode(avctx, frame, &(int){0});
     418             : }
     419             : 
     420      791171 : int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
     421             : {
     422      791171 :     av_packet_unref(avpkt);
     423             : 
     424      791171 :     if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
     425           0 :         return AVERROR(EINVAL);
     426             : 
     427      791171 :     if (avctx->codec->receive_packet) {
     428           0 :         if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
     429           0 :             return AVERROR_EOF;
     430           0 :         return avctx->codec->receive_packet(avctx, avpkt);
     431             :     }
     432             : 
     433             :     // Emulation via old API.
     434             : 
     435      791171 :     if (!avctx->internal->buffer_pkt_valid) {
     436             :         int got_packet;
     437             :         int ret;
     438      403273 :         if (!avctx->internal->draining)
     439      801783 :             return AVERROR(EAGAIN);
     440        4628 :         ret = do_encode(avctx, NULL, &got_packet);
     441        4628 :         if (ret < 0)
     442           0 :             return ret;
     443        4628 :         if (ret >= 0 && !got_packet)
     444        4493 :             return AVERROR_EOF;
     445             :     }
     446             : 
     447      388033 :     av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
     448      388033 :     avctx->internal->buffer_pkt_valid = 0;
     449      388033 :     return 0;
     450             : }

Generated by: LCOV version 1.13